How should arrays be created for plot_surface?

I am trying to understand how to create arrays for use in plot_surface (in Axes3d). I tried to create a simple surface that processes the data of these 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 understand how they are interpreted - for example, there is nothing in z = 2 on my plot. Someone please explain exactly what values โ€‹โ€‹will be made to create a point that is for the line and finally the surface.

For example, I would like to build a surface that connects to the points of the line: [0,0,0] โ†’ [1,1,1] โ†’ [0,0,2] [0,0,0] โ†’ [1, - 1,1] โ†’ [0,0,2] and the surface between these lines. What should arrays do for plot_surface to get something like this?

+6
source share
2 answers

Understanding how gratings work in plot_surface is not easy. Therefore, I will first give a general explanation, and then I will explain how to convert the data to 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) . Fortunately, numpy.meshgrid will help. Embarrassed? Example:

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

The element (x[i], y[j]) is available as (X[j,i], Y[j,i]) . And its value Z , of course, Z[j,i] , which also needs to be determined.

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

I am applying the result of building 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

+2
source

If you understand correctly, you are trying to interpolate the surface through many points. I do not think plot_surface is the right function for this. But correct me if I am wrong. I think you should look for interpolation tools, perhaps those that are in scipy.interpolate. The result of the interpolation can then be constructed using plot_surface.

plot_surface can display a grid (with z values) in three-dimensional space based on x, y coordinates. The arrays of x and y are those created by numpy.meshgrid.

plot_surface example:

 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) 
+1
source

Source: https://habr.com/ru/post/920501/


All Articles