TourEiffel
TourEiffel

Reputation: 4434

Heatmap Cbar Adjust Color Gradient

I try to adjust my cbar color but struggling with it :

When I use : Center=None I have the following bar :

enter image description here

And When I use Center=0 , I have the following bar :

enter image description here

But it is not what I except : My goal is to have red starting at zero and being more and more red more the value is less than 0.

So the bar should look Like stuff like that :

enter image description here

So like this it will be more readable : Red Negative - Green Positive

(More its Red More its negative - More its Green more it is positive)

Below is my line of code :

ax = sns.heatmap(result, linewidth=0.1,cmap="RdYlGn",
cbar_kws=dict(ticks= [result.min()/2,.0,result.max()/2]),
center=None,vmin=result.min(),vmax=result.max(),robust=True,fmt="f")

It seems that vmin and vmax does not work as excepted

Upvotes: 2

Views: 1675

Answers (1)

JohanC
JohanC

Reputation: 80449

You seem to want a TwoSlopeNorm which compresses both parts differently. (The default uses symmetric coloring.)

from matplotlib import pyplot as plt
from matplotlib.colors import TwoSlopeNorm
import seaborn as sns

norm = TwoSlopeNorm(vcenter=0)
result = np.random.uniform(-0.01, 0.1, (10, 10))
ax = sns.heatmap(result, linewidth=0.1, cmap="RdYlGn", norm=norm,
                 cbar_kws=dict(ticks=[result.min() / 2, .0, result.max() / 2]),
                 robust=True, fmt="f")
plt.show()

sns.heatmap TwoSlopeNorm

Upvotes: 1

Related Questions