1

I followed this link to plot the 3D figure. My problem is I have already 3 lists for X, Y, Z

X.shape (n,) , Y.shape (n,) , Z.shape (n,)

How to pass these lists into surf = ax.plot_surface(X, Y, Z) as link show each of these variables have the following shape

X.shape (n,n) , Y.shape (n,n) , Z.shape (n,n)

If I passed these coordinate as them each one shape is (n,) then the 3d figure will appear as empty there is no points will be plotted!

I tried to use the np.meshgrid as following but this way will show only one surface in one plane instead of 3d points!

X,Y,Z = np.meshgrid(X,Y,Z)

X = X[0]
Y = Y[0]
Z = Z[0]

fig = plt.figure()

ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z)
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Hana90
  • 943
  • 5
  • 17
  • 37
  • Can you tell us a little bit more about the content of your original X,Y,Z arrays, maybe they can be reshaped to fit the requirements of plot_surface. – Jan Kuiken Jun 04 '17 at 14:54

1 Answers1

2

The solution will depend on how the data is organized.

Data on regular grid

If the X and Y data already define a grid, they can be easily reshaped to a quadrilateral grid. E.g.

#x  y  z
 4  1  3
 6  1  8
 8  1 -9
 4  2 10
 6  2 -1
 8  2 -8
 4  3  8
 6  3 -9
 8  3  0
 4  4 -1
 6  4 -8
 8  4  8 

can plotted as a plot_surface using

ax = fig.gca(projection='3d')
ax.plot_surface(X.reshape(4,3), Y.reshape(4,3), Z.reshape(4,3))

Arbitrary data

(a) In case the data is not living on a quadrilateral grid, one can interpolate the data on a grid. One method to do so is provided by matplotlib itself, using matplotlib.mlab.griddata.

import matplotlib.mlab
xi = np.linspace(4, 8, num=10)
yi = np.linspace(1, 4, num=10)
zi = matplotlib.mlab.griddata(X, Y, Z, xi, yi, interp='linear')
ax.plot_surface(xi, yi, zi)

(b) Finally, one can plot a surface completely without the use of a quadrilateral grid. This can be done using plot_trisurf.

plt.plot_trisurf(X,Y,Z)

This answer is an adapted version of my answer for contour plots.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • what is these (4, 8, 10) value represent? – Hana90 Jun 05 '17 at 22:05
  • for reshape fun, there is error : ValueError: total size of new array must be unchanged – Hana90 Jun 05 '17 at 22:43
  • `linspace(4,8,10)` creates an array of 10 values between 4 and 8. The Value error tells you that the reshaping must take the same number of total elements, like in the example where you have an array of length 12 and reshape it into a 4 times 3 array. – ImportanceOfBeingErnest Jun 06 '17 at 07:49