The following example of the curve function in R,
curve(dgamma(x, 3, .1), add=T, lwd=2, col="orange"),
plots the curve for the probability density function of the dgamma continuous distribution. The equivalent to dgamma in Python is scipy.stats.dgamma.
How can I plot the same curve for the same distribution in Python? I would like this more than fitting a kernel density estimator (KDE), which tend to be inaccurate.
I don't think you have a curve equivalent in matplotlib or seaborn for that matter. You have to define a set of points and plot over it on the same device. In this case, since you are doing a histogram, it's getting a number of evenly spaced points between the min and max :
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
x = stats.gamma.rvs(a=3,scale=1/0.1,size=1000)
plt.hist(x,density=True)
xl = np.linspace(x.min(),x.max(),1000)
plt.plot(xl,stats.gamma.pdf(xl,a=3,scale=1/0.1))

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With