Python seaborn heatmap annotation after masking

I am creating a Seaborn heatmap with a given pandas framework which has numbers and nan. Something like that:

1 2 nan
4 nan nan
nan nan nan

      

I need a hot map to be annotated and I also need to mask / gray out all nans. I found a mask function and can use it like this:

mask = df.isnull()

      

Then I can create a heatmap like this:

sns.heatmap(df, mask = mask, annot = True, fmt = "g", cmap = "Blues")

      

The problem is that with the mask, it seems like the nan are converted to strings while the numbers remain as floats. I am getting the following exception:

ValueError: unknown format code 'g' for object of type 'str'

Something like this happens (partial plot before it throws an exception):

enter image description here

As you can tell, the first formatted cell is fine as it is floating point, but then when it hits the nano that is masked it seems like it is not a string and cannot be formatted.

Does anyone have any suggestions on how to comment on non-oiled data only?

thank

+3


source to share





All Articles