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.
Asked
Active
Viewed 1.6k times
4
-
What code have you tried so far? – James Nov 16 '16 at 02:06
1 Answers
8
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:-

Nikos Tavoularis
- 2,843
- 1
- 30
- 27

Anoopjk
- 103
- 4
-
1Not to be pedantic, but doesn't "3D Gaussian Distribution" imply that the input is 3D? This and the other SO question you linked treat 2D gaussian distributions... – jtb Dec 02 '21 at 16:58