概率表面图matplotlib

时间:2012-11-09 09:54:45

标签: python matplotlib

我有x和y的2d值,其范围从x - [1,5]和y - [0.1 - 0.5]

如何在matplotlib中绘制轴为x,y和P(y)的三维表面?

我在网上的matlab中找到了这样做的代码但是我无法理解它并因此将其转换为matplotlib ...(对于下面编写的代码,值的范围完全不同于我的要求)< / p>

  mu = [1 -1]; Sigma = [.9 .4; .4 .3];
  [X1,X2] = meshgrid(linspace(-1,3,25)', linspace(-3,1,25)');
  X = [X1(:) X2(:)];
  p = mvnpdf(X, mu, Sigma);
  surf(X1,X2,reshape(p,25,25));

有人可以帮我做matplotlib完全相同的事情(也许是plot_surface?)

1 个答案:

答案 0 :(得分:4)

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.mlab as mlab
import numpy as np

def P(X, Y):
    return mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)      

fig = plt.figure()
ax = fig.gca(projection = '3d')
jet = plt.get_cmap('jet')

x = np.linspace(-2, 2, 60)
y = np.linspace(-2, 2, 60)
X, Y = np.meshgrid(x, y)
Z = P(X, Y)
surf = ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1, cmap = jet, linewidth = 0)
ax.set_zlim3d(0, Z.max())

plt.show()

产量

enter image description here