Fit gamma distribution to histogram python
Your data does not appear to be gamma-distributed, but assuming it is, you could fit it like this: Show
I was surprised that I couldn't found this piece of code somewhere. What I basically wanted was to fit some theoretical distribution to my graph. If you are lucky, you should see something like this: In statistics, the Gamma distribution is often used to model probabilities related to waiting times. The following examples show how to use the scipy.stats.gamma() function to plot one or more Gamma distributions in Python. Example 1: Plot One Gamma DistributionThe following code shows how to plot a Gamma distribution with a shape parameter of 5 and a scale parameter of 3 in Python: import numpy as np import scipy.stats as stats import matplotlib.pyplot as plt #define x-axis values x = np.linspace (0, 40, 100) #calculate pdf of Gamma distribution for each x-value y = stats.gamma.pdf(x, a=5, scale=3) #create plot of Gamma distribution plt.plot(x, y) #display plot plt.show() The x-axis displays the potential values that a Gamma distributed random variable can take on and the y-axis shows the corresponding PDF values of the Gamma distribution with a shape parameter of 5 and scale parameter of 3. Example 2: Plot Multiple Gamma DistributionsThe following code shows how to plot multiple Gamma distributions with various shape and scale parameters: import numpy as np import scipy.stats as stats import matplotlib.pyplot as plt #define three Gamma distributions x = np.linspace(0, 40, 100) y1 = stats.gamma.pdf(x, a=5, scale=3) y2 = stats.gamma.pdf(x, a=2, scale=5) y3 = stats.gamma.pdf(x, a=4, scale=2) #add lines for each distribution plt.plot(x, y1, label=shape=5, scale=3') plt.plot(x, y2, label='shape=2, scale=5') plt.plot(x, y3, label='shape=4, scale=2') #add legend plt.legend() #display plot plt.show() Notice that the shape of the Gamma distribution can vary quite a bit depending on the shape and scale parameters. Related: How to Plot Multiple Lines in Matplotlib Additional ResourcesThe following tutorials explain how to plot other common distributions in Python: How to Plot a Normal Distribution in Python How do you fit a gamma distribution?To fit the gamma distribution to data and find parameter estimates, use gamfit , fitdist , or mle . Unlike gamfit and mle , which return parameter estimates, fitdist returns the fitted probability distribution object GammaDistribution . The object properties a and b store the parameter estimates.
How do you fit a normal distribution to a histogram in Python?How to fit a distribution to a histogram in Python. data = np. random. normal(0, 1, 1000) generate random normal dataset.. _, bins, _ = plt. hist(data, 20, density=1, alpha=0.5) create histogram from `data`. mu, sigma = scipy. stats. norm. fit(data). best_fit_line = scipy. stats. norm. ... . plt. plot(bins, best_fit_line). How do I make a histogram from a list in Python?MatPlotLib with Python. Make a list of numbers and assign it to a variable x.. Use the plt. hist() method to plot a histogram.. Compute and draw the histogram of *x*.. We can pass n-Dimensional arrays in the hist argument also.. To show the plotted figure, use the plt. show() method.. |