如何添加值的标签以显示在条形图中的条形上方:
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
对象进行操作。将值放在栏上方将是这样的:
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 在条形图列上方注释标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23591254/