I want to do the same thing as the answer to this question did, but not in MATLAB, but in Python with matplotlib. So far I've completed the 3D plot with the code
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()
But how can I project this to a 2D contour plot? I'm trying
plt.figure()
CS = plt.contour(X, Y, rv)
plt.clabel(CS, inline=1, fontsize=10)
but apparently this isn't right, since Z is not an array type (I get the error TypeError: float() argument must be a string or a number
for the line CS = plt.contour(X, Y, rv)
). How can I project the multivariate distribution to a 2D contour plot? Thank you!