Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 284
ytamer Avatar asked Sep 18 '18 19:09

ytamer


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")
    
like image 117
ImportanceOfBeingErnest Avatar answered Oct 02 '22 14:10

ImportanceOfBeingErnest