Python - Seaborn:修改热图图例

Python - Seaborn: Modifying the heatmap legend

我刚刚创建了以下热图。

在图例中,最大值 (vmax) 设置为 0.10。我这样做是因为我想避免为更多 "extreme" 值着色。但是在图例中,是不是可以修改一下写成">=0.10" 所以加一个"greater or equal to"?

所以这是一个非常 hacky 的解决方案,我认为几乎肯定有更聪明的方法来做到这一点,希望 @mwaskom 可以权衡一下,但我能够通过显式地将它作为参数传递来访问颜色条对象像这样调用热图函数:

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)

制作这个:

我能够在 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 的回复中提取了一些信息。