gpt4 book ai didi

python - 在 Seaborn 中向热图注释添加单位

转载 作者:太空狗 更新时间:2023-10-29 19:33:35 27 4
gpt4 key购买 nike

我试图在 Seaborn 中将百分比表显示为热图:

sns.heatmap(S, annot=True, fmt=".1f", linewidths=1.0, square=1)

但是,我希望百分号出现在热图注释中的每个数字之后。 fmt 标志似乎只接受数字格式说明符。有没有办法在 Seaborn 中或通过一些 matplotlib 调整来做到这一点?

最佳答案

您必须遍历热图的所有文本值并添加 % 符号:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from matplotlib.ticker import FuncFormatter

sns.set()
fig, ax0 = plt.subplots(1,1)
data = np.array([[10.01,20.20],[-0.23,0.],[23.1049,-100.000000]])
ax = sns.heatmap(data, annot=True, fmt = '.1f', square=1, linewidth=1.)
for t in ax.texts: t.set_text(t.get_text() + " %")
plt.show()

enter image description here

关于python - 在 Seaborn 中向热图注释添加单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37602885/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com