Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should arrays for plot_surface be built?

Tags:

matplotlib

I'm trying to understand how to build arrays for use in plot_surface (in Axes3d). I tried to build a simple surface manipulating data of those arrays:

In [106]: x
Out[106]: 
array([[0, 0],
       [0, 1],
       [0, 0]])

In [107]: y
Out[107]: 
array([[0, 0],
       [1, 1],
       [0, 0]])

In [108]: z
Out[108]: 
array([[0, 0],
       [1, 1],
       [2, 2]])

But I can't figure out how they are interpreted - for example there is nothing in z=2 on my plot. Anybody please explain exactly which values will be taken to make point, which for line and finally surface.

For example I would like to build a surface that would connect with lines points: [0,0,0]->[1,1,1]->[0,0,2] [0,0,0]->[1,-1,1]->[0,0,2] and a surface between those lines. What should arrays for plot_surface look like to get something like this?

like image 421
Adam Pierzchała Avatar asked Jul 14 '12 19:07

Adam Pierzchała


People also ask

How do you plot a 2D surface in Python?

Surface plots are created by using ax. plot_surface() function. where X and Y are 2D arrays of points of x and y while Z is a 2D array of heights.

How do you plot a 3D surface plot in Python?

We could plot 3D surfaces in Python too, the function to plot the 3D surfaces is plot_surface(X,Y,Z), where X and Y are the output arrays from meshgrid, and Z=f(X,Y) or Z(i,j)=f(X(i,j),Y(i,j)). The most common surface plotting functions are surf and contour. TRY IT!

What is plot_ surface?

A surface plot is like a wireframe plot, but each face of the wireframe is a filled polygon. Adding a colormap to the filled polygons can aid perception of the topology of the surface being visualized: ax = plt. axes(projection='3d') ax. plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='viridis', edgecolor='none') ax.


2 Answers

Understanding how the grids in plot_surface work is not easy. So first I'll give a general explanation, and then I'll explain how to convert the data in your case. If you have an array of N x values and an array of M y values, you need to create two grids of x and y values of dimension (M,N) each. Fortunately numpy.meshgrid will help. Confused? See an example:

x = np.arange(3)
y=np.arange(1,5)
X, Y = np.meshgrid(x,y)

The element (x[i], y[j]) is accessed as (X[j,i], Y[j,i]). And its Z value is, of course, Z[j,i], which you also need to define.

Having said that, your data does produce a point of the surface in (0,0,2), as expected. In fact, there are two points at that position, coming from coordinate indices (0,0,0) and (1,1,1).

I attach the result of plotting your arrays with:

fig = plt.figure()
ax=fig.add_subplot(1,1,1, projection='3d')
surf=ax.plot_surface(X, Y, Z)

enter image description here

like image 121
Ramon Crehuet Avatar answered Oct 05 '22 02:10

Ramon Crehuet


If I understand you correctly you try to interpolate a surface through a set of points. I don't think the plot_surface is the correct function for this. But correct me if I'm wrong. I think you should look for interpolation tools, probably those in scipy.interpolate. The result of the interpolation can then be plotted using plot_surface.

plot_surface is able to plot a grid (with z values) in 3D space based on x, y coordinates. The arrays of x and y are those created by numpy.meshgrid.

example of plot_surface:

import pylab as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

plt.ion()

x = np.arange(0,np.pi, 0.1)
y = x.copy()
z = np.sin(x).repeat(32).reshape(32,32)

X, Y = np.meshgrid(x,y)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X,Y,z, cmap=plt.cm.jet, cstride=1, rstride=1)
like image 34
Niels Avatar answered Oct 05 '22 04:10

Niels