Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R's curve function, but in Python, for plotting a continuous distribution

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.

like image 931
develarist Avatar asked Aug 03 '26 10:08

develarist


1 Answers

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))

enter image description here

like image 171
StupidWolf Avatar answered Aug 08 '26 23:08

StupidWolf



Donate For Us

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