Matplotlib: Histogram with bars inside grid lines...how??

Enigma Curry workbee at gmail.com
Thu Mar 30 05:48:58 CEST 2006


Thank you John. Your explanation helped a lot!

In case it helps anyone else in the future, here is my code for
*exactly* what I was after:

import pylab

def ryan_hist(data, bar_width, min_x, max_x):
    """
    Create a frequency histogram over a continuous interval

    min_x = the low end of the interval
    max_x = the high end of the interval

    bar_width = the width of the bars

    This will correctly align the bars of the histogram
    to the grid lines of the plot
    """
    #Make histogram with bars of width .9 and center
    #them on the integer values of the x-axis
    bins = pylab.nx.arange(1-(bar_width/2),max(data))
    n,bins,patches = pylab.hist(data, bins, width=bar_width)

    #Make Y axis integers up to highest n
    pylab.yticks(pylab.arange(sorted(n)[-1]))
    pylab.axis('scaled')
    pylab.xlim(0.5,6.5)
    pylab.grid()
    pylab.show()

#Create a historgram
data=[1,1,2,2,2,2,2,2,3,3,3,4,4,4,5,5,6,6,6]
bar_width = 0.9
ryan_hist(data,bar_width,min(data),max(data))




More information about the Python-list mailing list