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

Seaborn heatmap, format of annotations in non-diagonal cells

我使用 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。

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

参考: