How to assign custom color to masked cells in seaborn heatmap?

Viewed 4581

I have a dataset with values -4 to 4 and some nan values. I plot the heatmap using seaborn heatmap. Colormap I need to use is from red to white to blue. My problem is masked cells are also white/greyish which is hard to differentiate then values close to 0 in colormap.

Is there any way to assign nan values as black without plotting the heatmap twice?

1 Answers

You have two options.

  1. Use the bad value of the colormap. I.e. if masked values are set to nan, they would be shown in the color set to the colormap via

    colormap.set_bad("black") 
    
  2. Make the background of the axes black, such that values which are masked and hence not plotted appear as transparent with the background color see through,

    ax.set_facecolor("black")
    
Related