[Numpy-discussion] numpy histogram data

Doug Davis ddavis at ddavis.io
Sun Jun 6 12:39:49 EDT 2021


Resending after subscribing to the list since original reply was
rejected, that's what I get for reading the mailing list from gmane
instead of actually subscribing :)

Keith Sloan <keith at sloan-home.co.uk> writes:

> Okay I have some data which I have created a scatter plot
> <http://numpy-discussion.10968.n7.nabble.com/file/t2643/A2D75C32-6DAF-4DA7-B7AE-9DE8C2B0D07A.jpeg> 
>
> I have also created a histogram of red Galaxy counts for redshift with
>
> RedEllipticalMasses.keep_columns(['uminusr','Z_1'])
> RedEllipticalMasses.sort(['Z_1','uminusr'])
> counts, bins = np.histogram(RedEllipticalMasses['Z_1'],bins=80)
> fig, ax = plt.subplots(1, 1, figsize=(12,8)) # make the figure with the size
> 10 x 8 inches
> fig.suptitle("2 - Histogram Count of Red Galaxies versus Redshift")
> plt.hist(bins[:-1], bins, weights=counts)
> plt.show()
> <http://numpy-discussion.10968.n7.nabble.com/file/t2643/05EA171B-3CA3-4834-83FE-31EEB60B744C.jpeg> 
>
> Is there a way to created the histogram data for a stacked histogram formed
> from different ranges of redness 'uminusr'?

if you have already calculated a few histograms (i.e. you have the
arrays of bin heights/counts), you can create a stacked histogram by
passing a sequence of centers as the "data", a sequence of counts as the
weights, and use stacked=True; for example:

counts = [redness1, redness2, redness3]
centers = [centers for _ in counts]

plt.hist(centers, bins=bins, weights=counts, stacked=True)

> Also is there an easy way of extracting the point information for the
> midpoints of each histogram column?

NumPy doesn't have a function for this, but since np.histogram returns
the bin edges you can get the centers pretty easily with:

centers = 0.5 * (edges[1:] + edges[:-1])

There's a nice object oriented histogramming library for Python called
boost-histogram: https://boost-histogram.readthedocs.io/en/latest/
It might be worth giving it a look.


More information about the NumPy-Discussion mailing list