在Seaborn中添加单位以热图注释

Jsl*_*Jsl 9 python matplotlib seaborn

我试图在Seaborn中显示一个百分比表作为热图:

sns.heatmap(S, annot=True, fmt=".1f", linewidths=1.0, square=1)
Run Code Online (Sandbox Code Playgroud)

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

Ser*_*ity 17

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

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 将 '.1f' 转换为 '.1%' 为我解决了这个问题。 (3认同)

Meg*_*raj 8

将".1f"替换为".1%".它应该解决你的问题.

sns.heatmap(S, annot=True, fmt=".1%", linewidths=1.0, square=1)
Run Code Online (Sandbox Code Playgroud)

  • 那么颜色图将从 0 到 1 而不是 0 到 100。 (2认同)