amirhossein sarfi
amirhossein sarfi

Reputation: 25

How to center a colorbar at a specific color for Seaborn Heatmap?

Is it possible to specify the color of center of colorbar in seaborn heatmap? As example the ceneter of colorbar of the following heat map is 70, and I want to be specified with black color instead of white. Thanks in advance.

the example heatmap

Upvotes: 1

Views: 1068

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40747

You can use DivergingNorm to specify an off-centered normalization. To create the cmap with black in the center, use LinearSegmentedColormap

from matplotlib.colors import LinearSegmentedColormap, DivergingNorm

cmap = LinearSegmentedColormap.from_list('BkR',['blue','black','red'])
norm = DivergingNorm(vmin=0, vcenter=70, vmax=100)

x,y = np.random.randint(0,100, size=(2,50))

plt.figure()
plt.scatter(x,y,c=y, norm=norm, cmap=cmap)
plt.colorbar()
plt.show()

enter image description here

Upvotes: 1

Related Questions