繁体   English   中英

更改 sns.heatmap 中特定标签的颜色?

[英]Change the color of specific labels in sns.heatmap?

有没有办法改变 sns.heatmap 中特定标签的颜色?

基于我对这个答案的尝试,我尝试了以下方法:

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

但得到:

---------------------------------------------------------------------------
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'

还有一个热图,其中ascii label 不是红色的。

您复制的答案是clustermap ,它返回一个带有多个轴的 object 。

在这里,您使用的是在单个轴上运行的heatmap 你应该写:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM