Seaborn热图,非对角单元格中注释的格式

时间:2020-08-08 23:20:51

标签: annotations format seaborn heatmap

我使用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 Heatmap with differently formatted annotations

我已经浏览过Seaborn文档,但是找不到任何将格式应用于非对角线单元注释的设置。有人知道怎么做吗?

编辑:例如,我希望将0.017设置为0.02,将0.0099设置为0.01,但是0.00应该为0。

1 个答案:

答案 0 :(得分:0)

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

enter image description here

参考:

change certain squares in a seaborn heatmap