1

I have 2d values of x and y which span from x - [ 1 , 5 ] and y - [0.1 - 0.5]

How can I plot the 3d surface where the axis are x , y and P(y) in matplotlib ?

I found out the code for doing so in matlab on net but I am unable to understand it and consequently convert it into matplotlib... ( the range of values is completely different for below written code as to what I require )

  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));

Can someone help me out in doing the exact same thing for matplotlib ( plot_surface perhaps ? )

1 Answer 1

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()

yields

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, 'x = np.linspace(-2, 2, 60) y = np.linspace(-2, 2, 60)' refer to the total space of X and Y ? Where would the values of my x and y go ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.