Python-Seaborn:修改热图图例

插头4

我刚刚创建了以下热图。 在此处输入图片说明

在图例中,最大值(vmax)设置为0.10。我这样做是因为我想避免为更多的“极端”值着色。但是,在图例中,是否可以对其进行修改并写入“> = 0.10”,所以添加“大于或等于”?

mburke05

因此,这是一个非常hacky的解决方案,并且认为几乎可以肯定有一种更聪明的方法,希望@mwaskom可以参与其中,但是我能够通过在调用heatmap函数时将其作为参数显式传递来访问颜色栏对象,例如所以:

import seaborn as sns; sns.set()
import numpy as np; np.random.seed(0)
from matplotlib import pyplot as plt

fig, ax = plt.subplots()
fig.set_size_inches(14, 7)
uniform_data = np.random.rand(10, 12)
cbar_ax = fig.add_axes([.92, .3, .02, .4])
sns.heatmap(uniform_data, ax=ax, cbar_ax=cbar_ax)

产生这个:

在此处输入图片说明

我能够在以下位置找到the虫ax.get_yticks()

In [41]: cbar_ax.get_yticks()
Out [41]: array([ 0.19823662,  0.39918933,  0.60014204,  0.80109475])

标签本身是字符串:

In [44]: [x.get_text() for x in cbar_ax.get_yticklabels()]
Out [44]: [u'0.2', u'0.4', u'0.6', u'0.8']

因此,我们可以简单地在yticklabels中更改最后一个元素的文本对象,并希望得到校正后的轴,这是我的最终代码:

fig, ax = plt.subplots()
fig.set_size_inches(14, 7)
uniform_data = np.random.rand(10, 12)
#add an axis to our plot for our cbar, tweak the numbers there to play with the sizing. 
cbar_ax = fig.add_axes([.92, .3, .02, .4])
#assign the cbar to be in that axis using the cbar_ax kw
sns.heatmap(uniform_data, ax=ax, cbar_ax=cbar_ax)

#hacky solution to change the highest (last) yticklabel
changed_val = ">= " + cbar_ax.get_yticklabels()[-1].get_text()

#make a new list of labels with the changed value.
labels = [x.get_text() for x in cbar_ax.get_yticklabels()[:-1]] + [changed_val]

#set the yticklabels to the new labels we just created. 
cbar_ax.set_yticklabels(labels)

产生:

在此处输入图片说明

关于此主题的一些其他资源可以在此处找到,我从mwaskom的响应中提取了一些信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章