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.
source to share
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)
source to share