[Numpy-discussion] numpy histogram data

Doug Davis ddavis at ddavis.io
Mon Jun 7 10:04:39 EDT 2021


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

> Thanks
>
> Not sure where I get centers & edges from

Every time you call np.histogram the second return will be an array of
edges; so you define centers anywhere after your first np.histogram
call, e.g. with your bins1 variable. (I can't tell if this is exactly
the case, but it looks like you are pre-defining your bin edges in the
`bins` variable, which would allow you to use that as well.)

>
> ##### Plot of Histogram of Stacked Counts of Red elliptical Galaxies versus
> Red Shift
> REMrange1 =
> RedEllipticalMasses[RedEllipticalMasses[RedEllipticalMasses['uminusr']>1.8]['uminusr']
> <2.0]
> print(len(REMrange1))
> counts1, bins1 = np.histogram(REMrange1['Z_1'],bins=bins)

After this line if bins is an array of bin edges it just gets copied to
bins1. (the edges return is useful when bins are defined with an integer
(total number) and range is a tuple (xmin, xmin)). So you can define
centers with:

centers = 0.5 * (bins1[1:] + bins1[:-1]


More information about the NumPy-Discussion mailing list