1

I have a table in pandas df, which has avg_sp and count1 as columns. I plotted a bar graph grouped by ranges and I also added a for loop for the value on top.

plt.figure(figsize=(12, 6))
df2 = df.groupby(pd.cut(df['avg_sp'], range(0, 110,10))).sum()  ['count1'].plot(kind='bar')
plt.xlabel('avg_sp')
plt.ylabel('browse count')

for p in df2.patches:
    df2.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005),rotation=90)

But I am not getting the right result, as shown below, it's getting mixed with x axis, is there any way to bring up the no.s a little?

enter image description here

enter image description here

i added the code which pirsquared suggested, but it is affecting only the top bar, and other remain same.

Shubham R
  • 7,382
  • 18
  • 53
  • 119

1 Answers1

3

consider the series s

s = pd.Series(np.random.randn(10000))
s.hist()

enter image description here


from matplotlib docs

def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height),
                ha='center', va='bottom')

ax = s.hist()
for c in ax.containers:
    autolabel(c)

enter image description here


same solution with ax.patches

ax = s.hist()
for rect in ax.patches:
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
            '%d' % int(height),
            ha='center', va='bottom')

enter image description here


rotated labels docs

ax = s.hist()
for rect in ax.patches:
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
            '%d' % int(height),
            ha='center', va='bottom', rotation=90)

enter image description here


I messed with the height settings a bit to get it where I'd like

ax = s.hist()
for rect in ax.patches:
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width()/2., 1.01*height+100,
            '%d' % int(height),
            ha='center', va='bottom', rotation=90)

enter image description here

piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • and rotation = 90 would work on this too? by the way,, thanks – Shubham R Nov 11 '16 at 08:34
  • in my **barplot** the height change is not happening, its nearly touching the x axis, even after running your edited post, only first bar is getting affected, remaining bars distance is still same – Shubham R Nov 11 '16 at 08:52
  • Try and run the same exact code to see if it works on the example I provided. If it doesn't work, that implies there is a difference in the versions of your libraries or something else system related. If it does work, that implies that you have a typo somewhere or that you have to take the height adjustment to more of an extreme like adding 200 instead of 100. – piRSquared Nov 11 '16 at 08:57
  • i used 200,300 and other combinations, but it is affecting only one bar! and not all the bars, i have added another snapshot of my plot after running your code – Shubham R Nov 11 '16 at 09:01
  • Notice that I'm adding in my last example. When just scaling with a multiplier, the very small numbers stay very small. That's why it looks like it only works for the right most bar. Because that's the largest and one that responds the most to a multiplier – piRSquared Nov 11 '16 at 09:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127901/discussion-between-s-ringne-and-pirsquared). – Shubham R Nov 11 '16 at 13:56