问题描述
R 中 example 的以下 curve function,
curve(dgamma(x,3,.1),add=T,lwd=2,col="orange")
,
绘制 dgamma
连续分布的概率密度函数曲线。 Python 中 dgamma
的等价物是 scipy.stats.dgamma
。
如何在 Python 中为相同的分布绘制相同的曲线?我希望这不仅仅是拟合核密度估计器 (KDE),后者往往不准确。
解决方法
我不认为您在 curve
或 matplotlib
中有 seaborn
等价物。您必须定义一组点并在同一设备上对其进行绘图。在这种情况下,由于您正在绘制直方图,因此会在最小值和最大值之间获得许多均匀间隔的点:
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))