Bobby Durrett
Bobby Durrett

Reputation: 1293

Why are the arguments to Matplotlib's plot_wireframe two dimensional arrays?

I cannot find a clear explanation of the X, Y, and Z arguments to Matplotlib's plot_wireframe function. I have been playing with their provided example wire3d_demo.py but I don't get why X, Y, and Z are two dimensional arrays. It is a pretty curved 3D picture but if you look at the X, Y, and Z arrays they are all size 120 by 120 2D arrays. Matplotlib's documentation has this terse description: "Data values". Can anyone elaborate on that a little bit? I like Matplotlib but its documentation can be a little sparse.

Updating the question based on the answer. Maybe the first dimension of the 2D array represents a line in 3D space and the wireframe is constructed to connect up all of the lines. In this example I draw two quarter circles in the x,y plane with z coordinates 1.0 apart:

temp_x = []
temp_y = []
temp_z1 = []
temp_z2 =[]

import math
# x^2+y^2=4
for i in range(201):
    x = 0.01 * i
    temp_x.append(x)
    y = math.sqrt(4-(x*x))
    temp_y.append(y)
    temp_z1.append(0.0)
    temp_z2.append(1.0)

X=np.array([temp_x,temp_x])
Y=np.array([temp_y,temp_y])
Z=np.array([temp_z1,temp_z2])

# Plot a basic wireframe.
ax.plot_wireframe(X, Y, Z)

two quarter circles connected by wire frame

I guess the first dimension is line number or maybe function number and the second is point number.

Upvotes: 3

Views: 1330

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339340

In plot_wireframe(X,Y,Z), X are the x coordinates, Y are the y coordinates and Z are the z coordinates. Those need to be 2D arrays. The reason is that the plotting function needs a 2D grid. Hence, (X[i,j], Y[i,j], Z[i,j]) defines a point in cartesian space. i,j are the grid indices.

Now the question might be, why do we need i,j as grid index, and cannot use a single 1D grid with an index k alone, such that X,Y,Z would be 1D arrays?
That is, because in the case of (X[k], Y[k], Z[k]) the information about which point lies next to which other point is lost. In the 2D grid, you can unambigously determine that (X[i,j], Y[i,j], Z[i,j]) is connected to (a neighbor of) (X[i+1,j], Y[i+1,j], Z[i+1,j]), as well as e.g. (X[i,j-1], Y[i,j-1], Z[i,j-1]).
In the 1D case, (X[k+1], Y[k+1], Z[k+1]) is clearly connected to (X[k], Y[k], Z[k]), but that gives you only two connections, while you need 4 connections in the wireframe.

Upvotes: 2

Sheldore
Sheldore

Reputation: 39052

Let me explain with some numbers. For each value of x, there is a range of y-values. Suppose you have total 9 points in 3-d surface. Each of them will have a x-value, a y-value and a corresponding z-value. Let's say x = 1, 2, 3 and y = 1, 2, 3.

Now for each value of x, there are 3 y-points because you have a sort of meshgrid

x = 1 --> y = 1, 2, 3     
x = 2 --> y = 1, 2, 3
x = 3 --> y = 1, 2, 3

Similarly, for each value of y, there are 3 x-points.

y = 1 --> x = 1, 2, 3
y = 2 --> x = 1, 2, 3
y = 3 --> x = 1, 2, 3

So total combinations of x and y and 9

(x, y) --> (1, 1), (1, 2), (1, 3), 
           (2, 1), (2, 2), (2, 3), 
           (3, 1), (3, 2), (3, 3) 

So the shape of x-values now becomes (3x3) because you have total 9 values. Similarly the shape of y-values becomes (3x3). Now for each of these (x, y) pair, you have a corresponding z-value in 3d space. Hence, your z-values also has to be a 3x3 array.

The same concept and explanation holds for any number of points, in your question this number being 120x120.

Upvotes: 2

Related Questions