如何在柱状图上显示数值计数

4

请问我该如何在我的柱状图上显示数量?谢谢。

sns.countplot(data['target'])
plt.plot(0, label ="0 = No")
plt.plot(1, label ="1 = Yes")
plt.xlabel("Target")
plt.ylabel("count")
plt.title("Target vrs count")
plt.legend()
plt.show()

enter image description here

1个回答

5

您可以使用matplotlib.text

from matplotlib import pyplot as plt
data = pd.DataFrame({'target': [1, 1, 1, 0, 0]})
sns.countplot(data.target);
for v in [0, 1]:
    plt.text(v, (data.target == v).sum(), str((data.target == v).sum()));

enter image description here


谢谢回复,但我收到一个错误,显示 TypeError Traceback (most recent call last) <ipython-input-129-7bb18d7a02d9> in <module>() 13 sns.countplot(data.target); 14 for v in [0, 1]: ---> 15 text(v, (data.target == v).sum(), str((data.target == v).sum())); TypeError: 'module' object is not callable - user7648646
@KwesiGene 你似乎有一个名为 text 的模块。尝试使用 from matplotlib import pyplot as plt,然后使用 plt.text - Ami Tavory
抱歉,我尝试了但仍然出现错误,提示文本不可用。错误链接:https://drive.google.com/open?id=1HPWbXLqisCpi5gu4h5wgqnHTdKPL_-Mp - user7648646
@KwesiGene 我更新了答案,以确切地展示我的意思。 - Ami Tavory

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接