9

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?

user517696
  • 2,472
  • 7
  • 24
  • 35
  • 1
    Possible duplicate of [Seaborn: countplot() with frequencies](http://stackoverflow.com/questions/33179122/seaborn-countplot-with-frequencies) – tmdavison Feb 10 '17 at 13:06

1 Answers1

6

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

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • tried your suggestion but the number does not move up. So in your coding `v.umpires + 0.2` does not work for me, the value is directly on top. Configuring the value of `i` moves the number left or right but no luck with the hight. Any idea why? – MaMo Apr 13 '18 at 09:05
  • @MaMo - Hard to know, I just test it in spyder with `matplotlib: 2.0.0` and `pandas: 0.22.0` and working nice. – jezrael Apr 13 '18 at 09:07
  • @MaMo - Problem with sample data from answer too? – jezrael Apr 13 '18 at 09:08
  • no, those work. My coding is almost the same except of the values and if I print what you named `s`, I also get a list looking like yours. No clue what is wrong especially because configuring `i` works – MaMo Apr 13 '18 at 09:30
  • @MaMo - unfortunately I have no idea :( – jezrael Apr 13 '18 at 09:31