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'? Also is there an easy way of extracting the point information for the midpoints of each histogram column? Thanks -- Sent from: http://numpy-discussion.10968.n7.nabble.com/
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@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.
Thanks Not sure where I get centers & edges from ##### 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) REMrange2a = RedEllipticalMasses[RedEllipticalMasses['uminusr']>2.0] REMrange2 = REMrange2a[REMrange2a['uminusr'] <2.2] counts2, bins2 = np.histogram(REMrange2['Z_1'],bins=bins) REMrange3a = RedEllipticalMasses[RedEllipticalMasses['uminusr']>2.2] REMrange3 = REMrange3a[REMrange3a['uminusr'] <2.4] counts3, bins3 = np.histogram(REMrange3['Z_1'],bins=bins) REMrange4a = RedEllipticalMasses[RedEllipticalMasses['uminusr']>2.4] REMrange4 = REMrange4a[REMrange4a['uminusr'] <2.6] counts4, bins4 = np.histogram(REMrange4['Z_1'],bins=bins) REMrange5a = RedEllipticalMasses[RedEllipticalMasses['uminusr']>2.6] REMrange5 = REMrange5a[REMrange5a['uminusr'] <2.8] counts5, bins5 = np.histogram(REMrange5['Z_1'],bins=bins) counts = [counts1, counts2, counts3, counts4, counts5] centers = [centers for _ in counts] fig, ax = plt.subplots(1, 1, figsize=(12,8)) # make the figure with the size 10 x 8 inches fig.suptitle("2 - Histogram Counts of Red Galaxies versus Redshift") ax.set_xlabel('Redshift Z') ax.set_ylabel('Counts of Galaxies') plt.hist(centers, bins=bins, weights=counts, stacked=True) plt.show() Barfs on NameError Traceback (most recent call last) <ipython-input-19-4592c1912a76> in <module> 53 counts5, bins5 = np.histogram(REMrange5['Z_1'],bins=bins) 54 counts = [counts1, counts2, counts3, counts4, counts5] ---> 55 centers = [centers for _ in counts] 56 fig, ax = plt.subplots(1, 1, figsize=(12,8)) # make the figure with the size 10 x 8 inches 57 fig.suptitle("2 - Histogram Counts of Red Galaxies versus Redshift") -- Sent from: http://numpy-discussion.10968.n7.nabble.com/
Keith Sloan <keith@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]
Thanks Okay trying to understand the data being returned. I have counts, bins = np.histogram(RedEllipticalMasses['Z_1'],bins=80) If I print lengths I get RedEllipticalMasses is 2514 bins = 81 and counts is 5 ( It is 5 Arrays each of length 80) Okay I can get centers with centers = 0.5 * (bins[1:] + bins[:-1] As you advised But unclear how I would get their heights and why counts has 5 arrays ? -- Sent from: http://numpy-discussion.10968.n7.nabble.com/
Keith Sloan <keith@sloan-home.co.uk> writes:
Thanks
Okay trying to understand the data being returned. I have
counts, bins = np.histogram(RedEllipticalMasses['Z_1'],bins=80)
If I print lengths I get RedEllipticalMasses is 2514 bins = 81 and counts is 5 ( It is 5 Arrays each of length 80)
Okay I can get centers with centers = 0.5 * (bins[1:] + bins[:-1]
As you advised But unclear how I would get their heights and why counts has 5 arrays ?
Not having a clear understanding of your dataset.. I'm not quit sure why you're seeing the results you described. Nevertheless if you define bins with an integer, the len(counts) should be equal to that number and len bins should indeed be that number + 1. Perhaps you have a mistake somewhere else in your code?
There really is not a lot of other code. RedEllipticalMasses is an Astropy Table read in from a fits file from astropy.table import Table, join import numpy as np RedEllipticalMasses = Table.read('../../GAMA_Data/REMassEClassEmeasure.fits') RedEllipticalMasses['Z_1'] is a single column name 'Z_1' If. I print its type I get <class 'astropy.table.table.Table'> -- Sent from: http://numpy-discussion.10968.n7.nabble.com/
participants (2)
-
Doug Davis
-
Keith Sloan