Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

python - seaborn don't give color in heatmap if cell value is higher than 0.05

I have created heatmap and I want to put a condition that if cell value is higher than 0.05 it will get specific color that is not in the cmap. Right now what I do is to define vmin and vmax but the problem is that the vmax still get the 0.5 or 0.6 values and I can't be sure that the condition is really kept.

green=sns.light_palette("seagreen",reverse=True,as_cmap=True)
sns.set(rc={'figure.figsize':(18.7,3.27)})
sns.heatmap(fhtmp,square=True,cmap=green,linewidths=.5,vmin=0, vmax=0.05)

enter image description here

as you can see here, I can't really know if the ones that are purple are equal to 0.05 or more than 0.05, I would like to be able to distinguish so if value is greater than 0.05 the cell will have different color such as white or gray.

Edit: I have managed to use kind of mask but green has one tone and the mask has serveral colores.

sns.set(rc={'figure.figsize':(18.7,3.27)})
ax = sns.heatmap(fhtmp, cmap=green, center=0.8, square=True, 
                 linewidth=.5, vmin=0, vmax=0.05)
ax = sns.heatmap(fhtmp, mask=fhtmp < 0.053, cmap='Blues', square=True, annot=False, vmin=0.053, vmax=0.53, cbar=False, ax=ax)
plt.show()

enter image description here so this is what I get this is still not the desired results


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use set_over() on your colormap. E.g. green.set_over('lightgrey'). Similar functions are set_under() and set_bad().

The extend keyword can show these extra colors in the colorbar (default as little triangles).

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

green = sns.light_palette("seagreen", reverse=True, as_cmap=True)
green.set_over('tomato')
sns.set(rc={'figure.figsize': (18.7, 3.27)})
sns.heatmap(np.random.uniform(0, 0.07, (1, 20)), square=True, linewidths=.5, annot=True, fmt='.3f',
            cmap=green, vmin=0, vmax=0.05, cbar_kws={'extend': 'max'})
plt.show()

example plot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...