How to assign custom color to masked cells in seaborn heatmap?
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?
1 Answer
1
You have two options.
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
bad
nan
colormap.set_bad("black")
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")
You would need a matplotlib colormap, but seaborn can use matplotlib colormaps as well, so if that doesn't work out of the box the conversion should be straight forward.
– ImportanceOfBeingErnest
Sep 18 '18 at 19:27
Thanks @ImportanceOfBeingErnest
– ytamer
Sep 18 '18 at 19:32
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Can I set bad value even if I use a color palette from seaborn color palette gallery?
– ytamer
Sep 18 '18 at 19:23