I am looking into the matplotlib module to plot my data. The graph that I am interested is in the following code. Now I want to mention some labels over the each bar plot mentioning some string and values is there any function to do?

Also I want to manually specify the values in y axis. Say for example I need the values to be distributed by the increments of 0.6 rather than say 0.5 (which the program automatically determines)


#!/usr/bin/env python
# a bar plot with errorbars
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()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, width, color='r')

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

# add some
ax.set_title('Scores by group and gender')
ax.set_xticklabels( ('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()
        print height
        ax.text(rect.get_x()+rect.get_width()/2., 2.05*height, '%d'%int(height),
                ha='center', va='bottom')


This article has been dead for over six months. Start a new discussion instead.