简体   繁体   中英

Visualizing a multivariate normal distribution in 3D with python

Does anyone know how to make a pretty visualization of the PDF of multivariate (bivariate for simply) normal distribution, with each variable's distribution is projected, like the below figure? Thanks in advance.

Source of the figure: 3D 绘图 from this thesis .

This plot is almost certainly produced using matplotlib . Take a look at their tutorials . Stack Overflow also has a matplotlib tag .

To plot in 3D you need to use themplot3d toolkit.

try this script

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

def gauss1(x):
    return np.exp(-(x**2))

def gauss(x, y):
    return gauss1(x)*gauss1(2*y)

fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = gauss(X, Y)

ax.plot_surface(X, Y, Z, rstride=2, cstride=2, alpha=0.4, cmap=cm.coolwarm)
cset = ax.contourf(X, Y, Z, zdir='x', offset=-4, cmap=cm.coolwarm)
cset = ax.contourf(X, Y, Z, zdir='y', offset=4, cmap=cm.coolwarm)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

This produces the following result

enter image description here

I am not sure how to plot only the contour lines for the projections, but at least is something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM