1

I am relatively new to python. I am doing different types of plots from data I have in a file. I successfully did a contour plot, and wanted to plot that same data on a 3D plot. So that data is already in X, Y and Z arrays. There are Nx X values, Ny Y values and Z is an (Nx,Ny) array. The most recent version of code I am trying is the following:

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

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.RdBu)
plt.show()

So, previously X, Y, and Z are gathered from a file into the arrays I just mentioned above. But when I try to run this, I get the following:

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.RdBu)
File "/....../axes3d.py", line 1358, in plot_surface
X.shape = (rows,cols)
ValueError: total size of new array must be unchanged

Is it that for some reason X should be a 2 dimensional array? or do I need to reshape X to be a column vector instead of row or something like that? I really don't get why it works for the contours but not for the 3d plot. Any comments will be appreciated. thanks.

1 Answer 1

2

Yes, X and Y should be 2-dimensional.

Try:

import numpy as np
X2D, Y2D = np.meshgrid(X,Y)
ax.plot_surface(X2D, Y2D, Z, rstride=1, cstride=1, cmap=cm.RdBu)

Hope this works

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

Comments

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.