如何用matplotlib绘制三维高斯分布

2024-04-24 13:37:14 发布

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

得到了三维高斯分布的均值和sigmas,然后用python代码绘制三维分布图,得到分布图。


Tags: 代码分布图绘制均值sigmas
1条回答
网友
1楼 · 发布于 2024-04-24 13:37:14

这是基于mpl_工具包的文档和基于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. Generating 3D Gaussian distribution in Python

  2. http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.h

相关问题 更多 >