如何使用 python 绘制高斯 pdf 的轮廓?

问题描述

我有旧的 Faithfutl 数据集,我想复制这个图:

enter image description here

我不知道如何绘制它。我有均值和方差。我知道如何用多元高斯来做到这一点,但对于单变量我有这个错误:TypeError: Input z must be 2D,not 3D

这是我的代码

t = np.linspace(1.5,6,100)

h = np.linspace(1.2,100)
w_0,w_1 = np.meshgrid(t,h)

z = stats.norm(u_ml,sig_ml).pdf(np.dstack((w_0,w_1)))

plt.contourf(t,h,z,cmap='plasma')

解决方法

好吧,我想出了代码:

t = np.linspace(1,6,100)
h = np.linspace(30,100,100)

w_0,w_1 = np.meshgrid(t,h)
z = stats.multivariate_normal(biv_mean,biv_cov).pdf(np.dstack((w_0,w_1)))
plt.figure(figsize=(10,5))
plt.plot(eruptions,waiting,'go')
plt.title("Duration of the eruption in minutes vs  the duration in minutes until the next eruption.")
plt.ylabel("waiting")
plt.xlabel("eruption duration")
plt.contour(t,h,z)
plt.show()