【问题标题】:Add Labels to a bar chart Python将标签添加到条形图 Python
【发布时间】:2021-03-27 08:04:46
【问题描述】:

我正在尝试将标签添加到作为图像附加的下方条形图中,并且我正在使用以下代码 有人可以帮忙吗?

#Count of Labels
Count = data.groupby(['kmeans']).size().reset_index(name='counts')
Count
Count.plot(x="kmeans", y=["counts"], kind="bar")

plt.title("Clustering Output grouped by Labels")
plt.xlabel("Labels_Predicted")
plt.ylabel("Total data points")
plt.savefig('label_count_img.png', dpi=1200)

【问题讨论】:

标签: python matplotlib label bar-chart


【解决方案1】:

我不确定你想做什么,所以我假设你想更改每个栏的名称。

我不知道您的数据是什么,所以我只是创建了自己的数据作为示例。不管怎样,我就是这样做的:

import matplotlib.pyplot as plt

x = ['Label 1', 'Label 2']
data = [419, 337]

plt.bar(x, data)
plt.title("Clustering Output grouped by Labels")
plt.xlabel("Labels_Predicted")
plt.ylabel("Total data points")

plt.show()

plt.savefig('label_count_img.png', dpi=1200)

【讨论】:

  • 您好,感谢您的回复。我在我的问题中粘贴的代码工作正常,但我只想在栏中显示标签(第一个栏中为 419,第二个栏中为 337)
猜你喜欢
  • 2021-02-20
  • 2011-01-09
  • 2014-07-04
  • 2018-08-14
  • 1970-01-01
  • 2014-12-07
  • 2021-09-20
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多