2017-02-05 3 views
0

Ich möchte das gleiche tun, wie die Antwort auf this Frage tat, aber nicht in MATLAB, sondern in Python mit Matplotlib. Bisher habe ich den 3D-Plot mit dem Code abgeschlossenProjektion der multivariaten Verteilung auf 2D-Plot?

import numpy as np 
import matplotlib.pyplot as plt 
from scipy.stats import multivariate_normal 
from mpl_toolkits.mplot3d import Axes3D 

mu_x = 0 
mu_y = 0 

x = np.linspace(-10,10,500) 
y = np.linspace(-10,10,500) 
X, Y = np.meshgrid(x,y) 
pos = np.empty(X.shape + (2,)) 
pos[:, :, 0] = X; pos[:, :, 1] = Y 
rv = multivariate_normal([mu_x, mu_y], [[1, 0.8], [0.8, 1]]) 

fig = plt.figure(figsize=(10,5)) 
ax = fig.gca(projection='3d') 
ax.plot_surface(X, Y, rv.pdf(pos),cmap='viridis',linewidth=0) 
ax.set_xlabel('X axis') 
ax.set_ylabel('Y axis') 
ax.set_zlabel('Z axis') 
ax.auto_scale_xyz([-10, 10], [-10, 10], [0, 0.5]) 
plt.show() 

Aber wie kann ich projizieren diese auf eine 2D-Konturdiagramm? Ich versuche

plt.figure() 
CS = plt.contour(X, Y, rv) 
plt.clabel(CS, inline=1, fontsize=10) 

aber anscheinend das ist nicht richtig, da Z kein Array-Typ ist (erhalte ich die Fehler TypeError: float() argument must be a string or a number für die Linie CS = plt.contour(X, Y, rv)). Wie kann ich die multivariate Verteilung auf ein 2D-Konturdiagramm projizieren? Vielen Dank!

Antwort

0

Sie müssen das gleiche Array an contour liefern, wie Sie es an plot_surface liefern.

CS = plt.contour(X, Y, rv.pdf(pos)) 
Verwandte Themen