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

josef.pktd at gmail.com josef.pktd at gmail.com
Fri Nov 27 09:33:32 EST 2009


On Fri, Nov 27, 2009 at 8:43 AM, Wayne Watson
<sierra_mtnview at sbcglobal.net> wrote:
> Thanks. That sounds like it should help a lot. Finding meaningful
> examples anywhere hasn't been easy. I thought I'd look through Amazon
> for books on Python and scientific uses. I found almost all were written
> by authors outside the US, and none seemed to talk about items like
> matplotlib. Ezdraw or something like that was often cited. I'm
> definitely in a learning stage, and much of what I need is in graphics
> to support some data analysis that I'm doing.
>
> Glad to hear it can gather bins into groups. It would be very
> disappointing if such a mechanism did not  exist. In the distant past,
> I've all too often had to write my own histogram programs for this,
> FORTRAN, etc.  My data is from a 640x480 collection of b/w pixels, which
> a processor has binned from 0-255, so I don't want repeat doing a
> histogram on 307K data points.
>
> Vincent Schut wrote:
>> 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?
>>>
>>>
>> Wayne,
>>
>> 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.

Numpy_Example_List_With_Doc is for an older version of numpy and hasn't
been kept up to date. So if your results don't match up, then the function
might have changed and the official docs will have the current description.

from numpy import *
is not recommended anymore, it messes up the global namespace too much

Besides the the example list, I found
http://scipy.org/Numpy_Functions_by_Category very helpful, because it
gave a better overview of which functions do similar things. In the
current docs, "See Also" can be used now in a similar way.

Good luck,

Josef

>>
>> If you check out the histogram() function, you'll see it takes a 'bins='
>> argument:
>>
>> 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! :)
>>
>> Vincent.
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>
> --
>           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
>
>             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
>              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet
>
>                   350 350 350 350 350 350 350 350 350 350
>                     Make the number famous. See 350.org
>            The major event has passed, but keep the number alive.
>
>                    Web Page: <www.speckledwithstars.net/>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list