esperluette
esperluette

Reputation: 1902

How do I set heatmap colors with min and max values when the colorpalette is diverging?

I would like to set the max and min values of the scale of my color palette. In the example below I would like the scale of the color palette to go from -10 to 50 as if it was a sequential colormap. I don't really care 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:

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 vmin=-10 is ignored.

Upvotes: 4

Views: 8154

Answers (1)

Igor Raush
Igor Raush

Reputation: 15240

From the docs (for vmin, vmax),

When a diverging dataset is inferred, one of these values may be ignored.

You should use the center argument to specify the value at which to center the colormap, in conjunction with one of vmax or vmin to specify a limit.

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

Upvotes: 5

Related Questions