如何在计数图中显示条形顶部的计数值?

use*_*696 7 python matplotlib pandas seaborn

我已经为在板球锦标赛中获胜最多比赛的裁判制定了一个计票图.使用的代码是:

  ax=matches['umpires'].value_counts().head(10).plot.bar(width=.8) 
Run Code Online (Sandbox Code Playgroud)

这会正确绘制条形图,但计数的确切值不会显示在每个条形图的顶部.

如何在每个栏上显示确切的数字?

jez*_*ael 5

我认为您需要循环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')
Run Code Online (Sandbox Code Playgroud)

图形