pandas 如何在计数图中的条形顶部显示计数值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42159250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show the count values on the top of a bar in a countplot?
提问by user517696
I have plotted a countplot for the umpires who have umpired the maximum number of matches in a cricket tournament. The code used is:
我为在板球锦标赛中裁判最多比赛数的裁判绘制了一个计数图。使用的代码是:
ax=matches['umpires'].value_counts().head(10).plot.bar(width=.8)
This plots the bar properly but the exact value of the count is not displayed on the top of each bar.
这将正确绘制条形图,但每个条形图的顶部并未显示准确的计数值。
How do I show the exact numbers on each bar?
如何在每个条形上显示确切数字?
采纳答案by jezrael
I think you need loop by iterrows
and add new labels:
我认为您需要循环iterrows
并添加新标签:
np.random.seed(100)
L = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
matches = pd.DataFrame(np.random.choice(L, size=(30,1)), columns=['umpires'])
#print (matches)
s = matches['umpires'].value_counts().head(10)
print (s)
C 5
Q 3
E 2
P 2
V 2
Y 2
H 1
D 1
M 1
J 1
Name: umpires, dtype: int64
ax=s.plot.bar(width=.8)
for i, v in s.reset_index().iterrows():
ax.text(i, v.umpires + 0.2 , v.umpires, color='red')