Reut
Reut

Reputation: 1592

Add the set_over color to cbar in heatmap

I have created hetmap in seaborn and I have set vmin, vmax and also set over value, so if the value of cell in heatmap is over 0.05 it gets the color silver. In the past, I have managed to create cbar that show also the set_over color inside it, like this:

enter image description here

The problem is that right now I don't remember how I did it in the past and couldn't find any explaination for in the documentation. so i'm lookinf for way to add this "grey triangle hat" to my cbar. This is what I have done by now:

green = sns.light_palette("green", reverse=True, as_cmap=True)
green.set_over('silver')

# common settings: linewidths for grid lines, hide colorbar, set square aspect
kwargs = dict(linewidths=1, cbar=True, square=True)

#I removed  here the part of the code that uses mask 
g=sns.heatmap(df_fruits, mask=mask, cmap=cmap, ax=ax,vmin=0,vmax=0.05, **kwargs)
    

g.set_facecolor('xkcd:silver')

enter image description here

My end goal: to be able to add this "grey triangle hat" to my cbar fir the set_over values (greater than 0.05)

Upvotes: 2

Views: 553

Answers (1)

JohanC
JohanC

Reputation: 80449

You can also make a dict for the colorbar keywords and set extend='max'.

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

np.random.seed(123)

green = sns.light_palette("green", reverse=True, as_cmap=True)
green.set_over('silver')

cbar_kws = dict(extend='max')
kwargs = dict(linewidths=1, square=True, cbar=True, cbar_kws=cbar_kws)
ax = sns.heatmap(np.random.rand(10, 10) / 10, cmap=green, vmin=0, vmax=0.05, **kwargs)

ax.set_facecolor('xkcd:silver')
plt.tight_layout()
plt.show()

seaborn heatmap

Upvotes: 1

Related Questions