如果单元格值大于0.05,seaborn不要在热图中给出颜色 [英] seaborn don't give color in heatmap if cell value is higher than 0.05

查看:27
本文介绍了如果单元格值大于0.05,seaborn不要在热图中给出颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了热图,我想提出一个条件,如果单元格值大于0.05,它将获得不在cmap中的特定颜色.现在,我要做的是定义vmin和vmax,但是问题是vmax仍然获得0.5或0.6的值,而且我不确定该条件是否得到真正保留.

  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) 

正如您在这里看到的,我真的不知道紫色的那些是否等于0.05或大于0.05,我希望能够区分,因此如果值大于0.05,则该单元格将具有不同的颜色,例如白色或灰色.

我设法使用了一种遮罩,但是绿色只有一种色调,并且遮罩具有服务器颜色.

  sns.set(rc = {'figure.figsize':( 18.7,3.27)})ax = sns.heatmap(fhtmp,cmap = green,center = 0.8,square = True,线宽= .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() 

所以这就是我得到的仍然不是理想的结果

解决方案

您可以使用

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)

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()

so this is what I get this is still not the desired results

解决方案

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()

这篇关于如果单元格值大于0.05,seaborn不要在热图中给出颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆