Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the count values on the top of a bar in a countplot?

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?

like image 676
user517696 Avatar asked Feb 10 '17 12:02

user517696


1 Answers

I think you need loop by iterrows and add new labels:

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')

graph

like image 126
jezrael Avatar answered Oct 13 '22 23:10

jezrael