如何解决如何在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)
这是我得到的: enter image description here
谢谢
解决方法
通过一个简单的循环解决问题,该循环在象限上迭代并仅在值大于 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
,
从 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='')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。