[英]Change the color of specific labels in sns.heatmap?
Is there a way to change the color of specific labels in sns.heatmap?有没有办法改变 sns.heatmap 中特定标签的颜色?
Basing my attempt on this answer , I tried the following:基于我对这个答案的尝试,我尝试了以下方法:
mask = np.zeros_like(cor, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
plt.figure(figsize=(12,10))
g = sns.heatmap(cor,
vmin=-1,
cmap='coolwarm',
annot=False,
mask = mask);
columns = df.columns
lut = dict(zip(columns, "rbg"))
row_colors = columns.map(lut)
for tick_label in g.ax_heatmap.axes.get_yticklabels():
tick_text = tick_label.get_text()
if tick_text == 'ascii':
column = columns.loc[int(tick_text)]
tick_label.set_color(lut[column])
But got:但得到:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-204-67642fc463ec> in <module>
12 row_colors = columns.map(lut)
13
---> 14 for tick_label in g.ax_heatmap.axes.get_yticklabels():
15 tick_text = tick_label.get_text()
16 if tick_text == 'ascii':
AttributeError: 'AxesSubplot' object has no attribute 'ax_heatmap'
And a heatmap where the ascii
label is not red.还有一个热图,其中
ascii
label 不是红色的。
The answer you copied was for a clustermap
, which returns an object with several axes.您复制的答案是
clustermap
,它返回一个带有多个轴的 object 。
Here, you are using heatmap
which operates on a single axes.在这里,您使用的是在单个轴上运行的
heatmap
。 You should write:你应该写:
ax = sns.heatmap(cor,
vmin=-1,
cmap='coolwarm',
annot=False,
mask = mask);
(...)
for tick_label in ax.get_yticklabels():
(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.