我使用Seaborn版本10.1生成了一个热图。我想用fmt='.2g'
但是,这似乎只影响对角线上的单元格。
import seaborn as sn
x = np.array([[0.99082, 0.00102, 0.0, 0.0],
[0.0, 0.98767, 0.00529, 0.00088],
[0.01744, 0.00097, 0.94961, 0.00291],
[0.00990, 0.00099, 0.00594, 0.94356]])
sn.heatmap(x, annot=True, fmt='.2g', cmap=plt.cm.Blues)
这是我得到的:
我已经浏览过Seaborn文档,但是找不到任何将格式应用于非对角线单元注释的设置。有人知道怎么做吗?
编辑:例如,我希望将0.017设置为0.02,将0.0099设置为0.01,但是0.00应该为0。
答案 0 :(得分:0)
str.format()
表示法的fmt='0.2f'
来设置所有注释的格式
'0.00'
的值更改为'0'
,请使用matplotlib.text
API。import seaborn as sns
x = np.array([[0.99082, 0.00102, 0.0, 0.0],
[0.0, 0.98767, 0.00529, 0.00088],
[0.01744, 0.00097, 0.94961, 0.00291],
[0.00990, 0.00099, 0.00594, 0.94356]])
sns.heatmap(x, annot=True, fmt='0.2f', cmap=plt.cm.Blues)
# update the desired text annotations
for text in ax.texts:
if text.get_text() == '0.00':
text.set_text('0')