How to plot 3d gaussian distribution with matplotlib?

时光毁灭记忆、已成空白 提交于 2021-01-24 13:54:07

问题


I have obtained the means and sigmas of 3d Gaussian distribution, then I want to plot the 3d distribution with python code, and obtain the distribution figure.


回答1:


This is based on documentation of mpl_toolkits and an answer on SO based on 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()

reference:-

  1. Generating 3D Gaussian distribution in Python

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



来源:https://stackoverflow.com/questions/40622203/how-to-plot-3d-gaussian-distribution-with-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!