0

I'm trying to plot a gamma distribution and I expect it to be normalized, but somehow, the values always diverge too much and I'm not getting the expected result.

My code so far looks like this:

from scipy.stats import gamma

a,loc,scale = gamma.fit(data)

x =np.linspace(gamma.ppf(0.1,a),gamma.ppf(0.99,a), data.size)
y = gamma.pdf(x,a,scale=scale,loc=loc)

plt.plot(x,y , color='#606060')

There are values very close to 0 in my data, which could be the main cause of the divergency. But even when I normalize the y values, I don't get what I expected. When I plot the gamma distribution, it diverges completely and looks like this:

Gamma plot

For context: I'm already plotting an histogram and the normal distribution of my data, and they look fine. I expected my final plot to look something like this: Desired plot

backer
  • 3
  • 2

1 Answers1

0
samples = stats.gamma.rvs(alpha, loc=loc, scale=scale, size=10000)    

plt.hist(samples)

does this look like you expect it?

Klops
  • 951
  • 6
  • 18
  • Thanks, but I actually wanted a line plot. I edited the question to add what I expect. I'm actually confused about whether should I normalize it or not. I thought the pdf would already return a 0-1 probability. – backer Mar 08 '23 at 13:49
  • a pdf is not bound to 0-1. so its natural to be above one. only the integral is one. so you are looking to change the plot by setting limits on the y axis? this can be done by `plt.ylims[0, 1]`? – Klops Mar 08 '23 at 14:07
  • or you might want to use a log scale for y axis or (best way) plot the pdf on a second y axis and the data histogram on the first. this will auto scale both to the frame – Klops Mar 08 '23 at 14:11