首页 > 解决方案 > 如何在 seaborn 热图中不仅标注大于 x 的值

问题描述

我不仅想在我的 seaborn 热图中标注大于 0.4 的值。

这是我的代码:

sns.set(font_scale=0.6)

sns.set(font_scale=0.6)
ax= sns.heatmap(corr, mask=mask, cmap=cmap, vmin=-1, vmax=+1, center=0,
            square=True, linewidths=.1, cbar_kws={"shrink": .82},annot=True,
            fmt='.1',annot_kws={"size":7})

ax.set_xticklabels(ax.get_xticklabels(), rotation=60)

这就是我得到的: 在此处输入图像描述

谢谢

标签: pythonannotationsseabornheatmappearson-correlation

解决方案


问题通过一个简单的循环来解决,该循环遍历象限并仅在值大于 0.4 时设置注释:

for t in ax.texts:
    if float(t.get_text())>=0.4:
        t.set_text(t.get_text()) #if the value is greater than 0.4 then I set the text 
    else:
        t.set_text("") # if not it sets an empty text

在此处输入图像描述


推荐阅读