如何绘制正态分布的三维图

2024-06-01 12:12:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在这个图中画出类似的东西,但没有成功

enter image description here

这可能是正态分布的3d图。有谁能帮我搞到这样的阴谋吗? 这是我正在尝试的代码

fig=plt.figure(figsize=(10,6))
ax=plt.axes(projection='3d')
x=np.arange(0,251,0.85)
y=np.arange(0,12,0.85)
X,Y=np.meshgrid(x,y)
for i in range(X.shape[0]):
    pos=np.empty(X.shape)
    R=(pos-0.85)/0.029
    Z=(1/0.29*np.sqrt(2*np.pi))*np.exp(-.5*R**2) #normal distribution
    ax.plot_surface(X,Y,Z) 

Tags: 代码posnpfigpltaxfigureshape
1条回答
网友
1楼 · 发布于 2024-06-01 12:12:11

下面是一个创建一个看起来有点相似的情节的想法。从300个随机中心开始,用非常小的带宽计算akde kernel^{}函数将高斯钟形放置在每个随机中心上,并对其求和

以下代码有点慢,因为曲面是在200x200个位置上计算和绘制的。为了缩小z轴,借用了this post中的代码

import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
import numpy as np

x = np.linspace(0, 251, 200)
y = np.linspace(0, 12, 200)
centers_x = np.random.uniform(x[0], x[-1], 300)
centers_y = np.random.uniform(y[0], y[-1], 300)
centers = np.vstack([centers_x, centers_y])
kernel = gaussian_kde(centers, bw_method=0.09)

X, Y = np.meshgrid(x, y)
positions = np.vstack([X.ravel(), Y.ravel()])
Z = np.reshape(kernel(positions).T, X.shape)

fig = plt.figure(figsize=(10, 6))
ax = plt.axes(projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap="hot", linewidth=0, rcount=200, ccount=200)
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([1, 1, 0.1, 1]))
ax.set_zticks([])
plt.show()

surface plot of 2d kde

相关问题 更多 >