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。
- 使用来自
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')
参考:
我使用 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。
- 使用来自
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')