# question #2 - labeled bar graphs

Is there an easy way to label bars with the value of the bar at that point? I am doing log bars and it would be nice to have them labeled.

I guess I can do this manually using text() and the values returned by bar(); is there an automatic way to do it?

Thanks!

There is nothing built in (though it would be a nice feature). Here
is a simple example:

import numpy as np
import matplotlib.pyplot as plt

N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)

ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars

fig = plt.figure()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)

womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)

ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width, ('G1', 'G2', 'G3', 'G4', 'G5') )

ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )

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

autolabel(rects1)
autolabel(rects2)
#fig.savefig('barchart_demo')
plt.show()

···

On Fri, Mar 21, 2008 at 4:18 PM, Simson Garfinkel <simsong@...1340...> wrote:

Is there an easy way to label bars with the value of the bar at that
point? I am doing log bars and it would be nice to have them labeled.

I guess I can do this manually using text() and the values returned by
bar(); is there an automatic way to do it?