如何在海生热图上仅标注大于x的值

2024-04-19 22:31:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我不想在我的seaborn热图上只标注大于0.4的值

这是我的密码:

sns.set(字体刻度=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)

这就是我得到的: enter image description here

多谢各位


Tags: true密码字体maskseabornaxcmap热图
2条回答

问题通过一个简单循环解决,该循环在象限上迭代,并仅在值大于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

enter image description here

从版本0.7.1开始,seaborn添加了一个与数据形状相同的标签数组选项(这里是documentation):

# Generate some array to plot
arr = np.arange(16).reshape(4, -1)

# Generate annotation labels array (of the same size as the heatmap data)- filling cells you don't want to annotate with an empty string ''
annot_labels = np.empty_like(arr, dtype=str)
annot_mask = arr > 8
annot_labels[annot_mask] = 'T'  

# Plot hearmap with the annotations
ax= sns.heatmap(arr, annot=annot_labels, fmt='')

enter image description here

相关问题 更多 >