noblerare
noblerare

Reputation: 11873

How do I show a plot of Random.gammavariate distribution?

I am a little confused at all the different distribution functions out there (i.e. numpy, scipy, matploglib.mlab, and random)

I am trying to get a sampling of gamma distribution numbers to use for my program and also plot out the distribution being used as well.

So far I am doing:

import random
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import scipy.special as sps
import numpy as np

k = 2.0
theta = 2.0
random.gammavariate(k, theta) # this gives me results of the gamma distribution

# but unfortunately, this doesn't work
# plt.plot(random.gammavariate(k, theta))
# plt.show()

# but this works somehow even if I don't specify 
# what bins and y is - just copied from a https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.gamma.html
# and seems to work
s = np.random.gamma(k, theta, 1000)
plt.plot(bins, y, linewidth=2, color='r')

Any help for explaining this to me is greatly appreciated.

Upvotes: 0

Views: 339

Answers (1)

Astrom
Astrom

Reputation: 767

You are trying to plot a curve (or scatter) of 1 dimensional data. You can show the distribution by plotting the histogram:

plt.hist(np.random.gamma(k, theta,100 ))

Note the 1000 will give you 1000 points. if you want to extract informations from the histogram like the bins:

 count, bins, ignored = plt.hist(np.random.gamma(k, theta, 100))

then you can plot with plt.plot that takes a 2D as input:

plt.plot(bins, y)

where y is given by:

import scipy.special as sps
import numpy as np
y = bins**(k-1)*(np.exp(-bins/theta) /(sps.gamma(k)*theta**k))

which is the gamma function.

Upvotes: 1

Related Questions