python - 使用 matplotlib 在条形图上添加值标签

标签 python csv pandas matplotlib bar-chart

当我打印时

graph_by_users = users.pivot(index='address', columns='used_at', values='users')

我明白

              address  used_at   time online
0               am.ru     2014    114.741944
1               am.ru     2015     50.945000
2             auto.ru     2014   2533.983889
3             auto.ru     2015   1923.157500
4            avito.ru     2014  23473.097500
5            avito.ru     2015  24357.936389
6       avtomarket.ru     2014     29.680278
7       avtomarket.ru     2015     26.646389
8   cars.mail.ru/sale     2014     58.737778
9   cars.mail.ru/sale     2015     46.466111
10            drom.ru     2014   3059.709722
11            drom.ru     2015   2695.590000
12              e1.ru     2014   7966.210278
13              e1.ru     2015   7767.182500
14        irr.ru/cars     2014     61.720278
15        irr.ru/cars     2015     37.132778

我需要打印条形图

ax = graph_by_duration.plot.bar(width=0.5, ax=axes[0])
ax1 = graph_by_duration.plot.bar(width=0.8)
rects = ax1.patches
labels = ["%d" % i for i in time['time online']]
for rect, label in zip(rects, labels):
    height = rect.get_height()
    ax1.text(rect.get_x() + rect.get_width()/2, height + 5, label,     
    ha='center', va='bottom')

my_graph 它将值放入 2014 以及之后的值 2015。但这是错误的,因为在我的数据框中,手段是混合的

最佳答案

我认为你混淆了你的标签。试试这个:

ax = graph_by_duration.plot(kind='bar', width=0.5)
[label.set_rotation(25) for label in ax.get_xticklabels()]

labels = [int(round(graph_by_duration.loc[i, y]))
          for y in graph_by_duration.columns.tolist()
          for i in graph_by_duration.index]

for rect, label in zip(rects, labels):
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width()/2, height + 5, label,     
    ha='center', va='bottom', rotation=15)

plt.show()

enter image description here

关于python - 使用 matplotlib 在条形图上添加值标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36111556/

相关文章:

python - python oop 中的委托(delegate)

python - Django RESTful API 错误 "type object ' User' has no attribute '_meta' "

python - 使用python删除矩阵中的相应行/列

python - 合并两个具有重复日期时间索引的 Pandas 系列

python - 在 flask 中找不到我的模型的路径

python - Django 中的动态逻辑查询构建器

python - 从 Flask 框架编写 CSV

excel - 从 Pig 导出到 CSV

swift - 如何将库包含到我的 Swift 项目中?

python - 创建具有重复值的数据框