如何使用matplotlib绘制3d高斯分布?

5
我已经得到了3D高斯分布的均值和标准差,我想使用Python代码绘制这个分布,并获取分布图。

你目前尝试了哪些代码? - James
1个回答

8

这是基于mpl_toolkits文档和SO上的一个答案,基于scipy multinormal pdf:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import numpy as np
from scipy.stats import multivariate_normal

x, y = np.mgrid[-1.0:1.0:30j, -1.0:1.0:30j]

# Need an (N, 2) array of (x, y) pairs.
xy = np.column_stack([x.flat, y.flat])

mu = np.array([0.0, 0.0])

sigma = np.array([.5, .5])
covariance = np.diag(sigma**2)

z = multivariate_normal.pdf(xy, mean=mu, cov=covariance)

# Reshape back to a (30, 30) grid.
z = z.reshape(x.shape)





fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')



ax.plot_surface(x,y,z)
#ax.plot_wireframe(x,y,z)

plt.show()

参考资料:

  1. 在Python中生成3D高斯分布

  2. https://matplotlib.org/tutorials/toolkits/mplot3d.html#sphx-glr-tutorials-toolkits-mplot3d-py


1
不想太苛刻,但“3D高斯分布”不是意味着输入是三维的吗?你提供的和其他SO问题都是处理二维高斯分布... - jtb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接