Home > Software engineering >  Seaborn heatmap cannot be plotted if most data is None
Seaborn heatmap cannot be plotted if most data is None

Time:04-13

Here is my code. I made half of the data in df to be None, so we get a triangle graph.

import seaborn as sns
import matplotlib.pyplot  as plt
import pandas as pd
df= {}
for a in [0,0.125,0.25,0.375,0.5,0.625,0.75,0.875,1]:
    df[a]={}
    for b in [0,0.125,0.25,0.375,0.5,0.625,0.75,0.875,1]:
        if a b<1:
            df[a][b]=None
        else:
            df[a][b]=a b
sns.heatmap(pd.DataFrame(df),square=True,annot=True,fmt=".1f",cmap="YlGnBu_r")
plt.show()

But if we changed the a b to be 1 or larger than 1, the script cannot work, like below:

import seaborn as sns
import matplotlib.pyplot  as plt
import pandas as pd
df= {}
for a in [0,0.125,0.25,0.375,0.5,0.625,0.75,0.875,1]:
    df[a]={}
    for b in [0,0.125,0.25,0.375,0.5,0.625,0.75,0.875,1]:
        if a b<1.01:
            df[a][b]=None
        else:
            df[a][b]=a b
sns.heatmap(pd.DataFrame(df),square=True,annot=True,fmt=".1f",cmap="YlGnBu_r")
plt.show()

It seems if more than half of the data is None, heatmap cannot plot such "unreasonable" graphs. Like the codes above, in my work I have a lot of data that has a None type (or void value), so how can I plot the heatmaps?

CodePudding user response:

The issue is the use of None instead of NaN.

None gets converted to NaN only if there are also numbers in the column:

pd.DataFrame([[1, None],[None, None]])

     0     1
0  1.0  None
1  NaN  None

In your example, the first column only has None items, which leads to an object dtype and causes heatmap to fail.

Change this bit:

        if a b<1.01:
            df[a][b]=float('nan')

Output:

enter image description here

  • Related