[Numpy-discussion] Producing a Histogram When Bins Are Known

Vincent Schut schut at sarvision.nl
Fri Nov 27 03:33:26 EST 2009

Wayne Watson wrote:
> I have a list that already has the frequencies from 0 to 255. However, 
> I'd like to make a histogram  that has say 32 bins whose ranges are 0-7, 
> 8-15, ... 248-255. Is it possible?

you might find the 'numpy example list with doc' webpage quite 
informative... http://www.scipy.org/Numpy_Example_List_With_Doc (give it 
some time to load, it's pretty large...)
For new users (I was one once...) it usually takes some time to find the 
usual suspects in numpy/scipy help and docs... This one page has really 
become unvaluable for me.

It gives you the docstrings for numpy functions, often including some 
example code.

If you check out the histogram() function, you'll see it takes a 'bins=' 

bins : int or sequence of scalars, optional
     If `bins` is an int, it defines the number of equal-width
     bins in the given range (10, by default). If `bins` is a sequence,
     it defines the bin edges, including the rightmost edge, allowing
     for non-uniform bin widths.

So, if your bins are known, you can pass it to numpy.histogram, either 
as number-of-bins (if equal width), if necessary combined with the 
'range=' parameter to specify the range to divide into equal bins, or as 
bin edges (e.g. in your case: (0, 8, 16, ... 256) or 
numpy.linspace(0,256,33) which will give you this range nicely.

If you don't specify the 'range=' parameter, it will check the min and 
max from your input data and use that as lower and upper bounds.

Good luck learning numpy! :)


More information about the NumPy-Discussion mailing list