Reputation: 190
The mplot3d function plot_surface
requires that the coordinates of the points defining the surface be input as three 2D arrays, X,Y,Z. Here is a working example that demonstrates the construction of these arrays. I express the x,y,z coordinates in spherical coordinates parameterized by the angles u and v.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
# generate the coordinates on the surface by parameterizing
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, 2 * np.pi, 100)
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z)
plt.show()
I believe that the coordinates represented by X,Y,Z are precisely the set of all triplets [X[i,j], Y[i,j], Z[i,j]]
indexed over i and j. (1) Is this correct?
Many of the functions I have written to construct the real plot take an argument that is a list of triplets, precisely in the form of the triplet above; i.e. points = [[x1,y1,z1], [x2,y2,z2], ..., [xn, yn, zn]]
. I anticipated that this input would be accepted by plot_surface
, but it is not. As I result I find myself needing to convert between the two representations fairly frequently, e.g. via
points = zip(*[x.flatten() for x in (X,Y,Z)])
I can do so, but I am worried that I ought to be using the 2D array format for my own functions as well. (2) Is there a reason for the array input format other than it is easy to program the construction of these arrays? For example, is it computationally more efficient?
Upvotes: 3
Views: 958
Reputation: 16364
The surface plot is probably drawn as a triangular mesh. Passing a 2D array of points makes it clear which points should be included in each triangle (probably [i][j]-[i+1][j]-[i][j+1]
and [i][j+1]-[i+1][j]-[i+1][j+1]
).
Upvotes: 2