How do I set the heatmap colors to minimum and maximum values ​​when the color palette diverges?

I would like to set the maximum and minimum scale values ​​of my color palette. In the example below, I would like the color palette scale to go from -10 to 50, as if it were a consistent color palette. It is not important for me to highlight where the numbers cross the "zero line".

import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np  
import pandas as pd  
import seaborn as sns

index = np.arange(0, 50)
data = np.random.uniform(low=-10, high=100, size=(50,50))
dft = pd.DataFrame(index=index, columns=index, data=data)
fig, ax = plt.subplots(figsize=(10,10))
cbar_ax = fig.add_axes([.905, 0.125, .05, 0.755])
ax = sns.heatmap(dft, linewidths=.5, cmap=cm.YlGnBu, cbar_kws={'label': 'label'},
                 ax=ax, square=True, cbar_ax=cbar_ax, center=55)
plt.show()

      

However, if I do this:

ax = sns.heatmap(dft, linewidths=.5, cmap=cm.YlGnBu, cbar_kws={'label': 'label'},
                 ax=ax, square=True, cbar_ax=cbar_ax, vmax=50, vmin=-10)

      

The color palette goes from -50 to 50 and is vmin=-10

ignored.

+3


source to share


1 answer


From the docs (for vmin, vmax ),

When a divergent dataset is returned, one of these values ​​can be ignored.



You must use an argument center

to specify a value that can be used to center the color code in combination with one of vmax

or vmin

to specify a limit.

vmin, vmax = -10, 50
sns.heatmap(..., center=(vmin + vmax) / 2., vmax=vmax)

      

+3


source







All Articles