Re: Trying to understand Distributions
Robert Kern <[email protected]> Sat, 3 Jul 2021 13:39:04 -0400
| Newsgroups | gmane.comp.python.scientific.user |
|---|---|
| Message-ID | <CAF6FJiv28rntH7DZNrzq5Xbvu+W_L0XZMupG4Bx1ADcH1kx8sA@mail.gmail.com> |
Instead of making your own bar chart from the results of `np.histogram()`, I recommend using `plt.hist(data, bins=binCount, density=True)`. The `density=True` argument is important to make the histogram Y axis commensurable with the PDF Y axis. On Sat, Jul 3, 2021 at 1:19 PM Keith Sloan <[email protected]> wrote: > I have a histogram that I would like to fit a gamma and rayleigh curve > ideally with a measure of fit. > I am struggling to have the histogram and distribution with the same > scales. > I tried to follow > https://stackoverflow.com/questions/40979643/python-how-to-fit-a-gamma-distribution-from-data > <https://github.com/scipy/scipy/issues/url> > and don't really know what I am doing > I would like to plot the histogram and distribution in one figure i.e. one > for gamma + histogram and another for rayleigh + histogram > and as I said some quantitative measure of the fit. > > Thanks > > > from astropy.table import Table, joinimport numpy as npimport matplotlib.pyplot as pltfrom matplotlib.pyplot import plot > RawMassEClassEmeasure = Table.read('../../GAMA_Data/REMassEClassEmeasure.fits')#print(RawMassEClassEmeasure.colnames)# CLEAN DATA#REMassEClassEmeasure = RawMassEClassEmeasure[RawMassEClassEmeasure['CountInCyl']> -500]RErange = RawMassEClassEmeasure[RawMassEClassEmeasure['CountInCyl']> -500]RErange1 = RErange[RErange['SurfaceDensity']< 50] > binCount = 30alphaVal = .3 > ##### uminusrfig = plt.figure(figsize=(12, 6), dpi=200)fig.suptitle('Plot - Histogram Red Galaxies for Elliptical Galaxies')#fig.legend(loc="upper right")#import scipy.stats as statsfrom scipy import statsxfield = 'uminusr'counts, bins = np.histogram(RErange1[xfield].data,bins=binCount)print(counts)ag, bg, cg =stats.gamma.fit(counts)print(ag, bg, cg) > ax1 = fig.add_subplot(3, 1, 1)ax1.set_ylabel('Galaxy Count')ax1.set_xlabel(xfield)counts, bins = np.histogram(RErange1[xfield].data,bins=binCount)ax1.hist(bins[:-1],bins, weights=counts)ax2 = fig.add_subplot(3, 1, 2)x = np.linspace(stats.gamma.ppf(0.1, ag),stats.gamma.ppf(0.99, ag), 243)ax2.plot(x, stats.gamma.pdf(x, ag),'r-', lw=5, alpha=0.6, label='gamma pdf') > param = stats.rayleigh.fit(counts) # distribution fitting# fitted distributionxx = np.linspace(0,45,1000)pdf_fitted = stats.rayleigh.pdf(xx,loc=param[0],scale=param[1])pdf = stats.rayleigh.pdf(xx,loc=0,scale=8.5) > ax3 = fig.add_subplot(3, 1, 3)plot(xx,pdf,'r-', lw=5, alpha=0.6, label='rayleigh pdf')plot(xx,pdf,'k-', label='Data')plt.bar(x[1:], counts)plt.show() > > [ 55 77 61 80 94 87 102 115 133 133 123 121 133 122 118 152 142 140 > 120 96 84 71 39 26 19 9 8 3 0 4] > 216.37114598925467 -636.6370861665209 3.3226700375837455 > > ---------------------------------------------------------------------------ValueError Traceback (most recent call last)/var/folders/cj/z259fmwd41dgzl8mq8nppwd40000gn/T/ipykernel_8820/1690958570.py in <module> 44 plot(xx,pdf,'r-', lw=5, alpha=0.6, label='rayleigh pdf') 45 plot(xx,pdf,'k-', label='Data')---> 46 plt.bar(x[1:], counts) 47 plt.show() 48 > /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py in bar(x, height, width, bottom, align, data, **kwargs) 2649 x, height, width=0.8, bottom=None, *, align='center', 2650 data=None, **kwargs):-> 2651 return gca().bar( 2652 x, height, width=width, bottom=bottom, align=align, 2653 **({"data": data} if data is not None else {}), **kwargs) > /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs) 1359 def inner(ax, *args, data=None, **kwargs): 1360 if data is None:-> 1361 return func(ax, *map(sanitize_sequence, args), **kwargs) 1362 1363 bound = new_sig.bind(ax, *args, **kwargs) > /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py in bar(self, x, height, width, bottom, align, **kwargs) 2302 yerr = self._convert_dx(yerr, y0, y, self.convert_yunits) 2303 -> 2304 x, height, width, y, linewidth, hatch = np.broadcast_arrays( 2305 # Make args iterable too. 2306 np.atleast_1d(x), height, width, y, linewidth, hatch) > <__array_function__ internals> in broadcast_arrays(*args, **kwargs) > /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/lib/stride_tricks.py in broadcast_arrays(subok, *args) 536 args = [np.array(_m, copy=False, subok=subok) for _m in args] 537 --> 538 shape = _broadcast_shape(*args) 539 540 if all(array.shape == shape for array in args): > /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/lib/stride_tricks.py in _broadcast_shape(*args) 418 # use the old-iterator because np.nditer does not handle size 0 arrays 419 # consistently--> 420 b = np.broadcast(*args[:32]) 421 # unfortunately, it cannot handle 32 or more arguments directly 422 for pos in range(32, len(args), 31): > ValueError: shape mismatch: objects cannot be broadcast to a single shape > > > > ========== Art & Ceramics ===========https://www.instagram.com/ksloan1952/ > > _______________________________________________ > SciPy-User mailing list > [email protected] > https://mail.python.org/mailman/listinfo/scipy-user > -- Robert Kern _______________________________________________ SciPy-User mailing list [email protected] https://mail.python.org/mailman/listinfo/scipy-user
pjgfhlnhgohdekdc.png
(image/png, 88.9 KB) - not displayed