条形图列上方的Python pandas/matplotlib注释标签 [英] Python pandas / matplotlib annotating labels above bar chart columns

查看:171
本文介绍了条形图列上方的Python pandas/matplotlib注释标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为要添加的值添加标签以显示在此处的条形图中的条形上方:

How do I add the label for the value to display above the bars in the bargraph here:

import pandas as pd
import matplotlib.pyplot as plt

df=pd.DataFrame({'Users': [ 'Bob', 'Jim', 'Ted', 'Jesus', 'James'],
                 'Score': [10,2,5,6,7],})

df = df.set_index('Users')
df.plot(kind='bar',  title='Scores')

plt.show()

推荐答案

捕获绘制绘图所在的轴,然后将其作为普通的matplotlib对象进行操作.将值放在条形上方将是这样的:

Capture the axis where the plot is drawn into, then manipulate it as a usual matplotlib object. Putting the value above the bar will be something like this:

ax = df.plot(kind='bar',  title='Scores')
ax.set_ylim(0, 12)
for i, label in enumerate(list(df.index)):
    score = df.ix[label]['Score']
    ax.annotate(str(score), (i, score + 0.2))

这篇关于条形图列上方的Python pandas/matplotlib注释标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆