Вопрос

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.

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top