Set max value for color bar on seaborn heatmap
Asked Answered
W

2

59

I need to set the max value on the seaborn heatmap cbar to 2. I've tried:

cbar_kws = { 'ticks' : [0, 2] }
sns.heatmap(tiles, robust=True, fmt="f", cmap= 'RdBu_r', cbar_kws = cbar_kws)

But this doesn't work and the documentation isn't very clear. How would I do this properly?

Waldo answered 18/11, 2015 at 12:12 Comment(0)
J
95

You want to use the vmin and vmax parameters for the heatmap, as described in the docs:

vmin, vmax : floats, optional

Values to anchor the colormap, otherwise they are inferred from the data and other keyword arguments.

sns.heatmap(tiles, robust=True, fmt="f", cmap='RdBu_r', vmin=0, vmax=2)
Joettajoette answered 18/11, 2015 at 12:23 Comment(0)
L
0

If for whatever reason, you want to set vmin and vmax after the heatmap() call, you can do so by calling set_clim() on the QuadMesh object (heatmap() calls pcolormesh() internally and its result resides in Axes.collections).

import seaborn as sns

ax = sns.heatmap([[x] for x in range(10)])
ax.collections[0].set_clim(0,2)               # set vmin and vmax

result

If you did not save the Axes that stores the heatmap, then

import matplotlib.pyplot as plt
sns.heatmap([[x] for x in range(10)])         # set vmin and vmax
plt.gca().collections[0].set_clim(0,2)
Leastwise answered 2/5, 2023 at 22:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.