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

John Hunter jdhunter at ace.bsd.uchicago.edu
Wed Mar 29 21:40:15 EST 2006


>>>>> "Enigma" == Enigma Curry <workbee at gmail.com> writes:

    Enigma> I'm playing around with matplotlib for the first time. I'm
    Enigma> trying to make a very simple histogram of values 1-6 and
    Enigma> how many times they occur in a sequence. However, after
    Enigma> about an hour of searching I cannot make the histogram
    Enigma> stay within the bounds of the grid lines.

    Enigma> Here is my example:

    Enigma> pylab.grid() x_values=[1,1,2,2,2,3,3,3,4,4,4,5,5,6,6,6]
    Enigma> pylab.hist(x_values,6) pylab.show()

    Enigma> This produced the following image:
    Enigma> http://enigmacurry.com/usenet/historgram-bars-not-in-grid-lines.png

    Enigma> Starting with bar number 2, it creeps into grid 1.. and
    Enigma> finally with bar number 5 it's almost entirely in grid
    Enigma> 4.. how do I make the bars stay in their own grid lines?

While exactly what you want is something of an enigma to me, I can
offer some advice and terminology.  The bars of hist make no attempt
to stay within the bounds of the grid lines...  The bars have as their
left most boundary the bins you choose for the histogram.  As a first
step, I suggest setting these bins explicitly, rather than letting the
hist command choose them automatically

from pylab import hist, xlim, nx, show

x_values= [1,1,2,2,2,3,3,3,4,4,4,5,5,6,6,6]
bins = nx.arange(0.5, 7.)
hist(x_values, bins)
xlim(0,6.5)
show()

The grid line locations are determined by the xtick locations, which
you can set with the xticks command.

Good luck!
JDH



More information about the Python-list mailing list