plot 3d in python using three lists

后端 未结 1 1930
眼角桃花
眼角桃花 2020-12-12 03:34

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,)

相关标签:
1条回答
  • 2020-12-12 04:00

    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.

    0 讨论(0)
提交回复
热议问题