admin 管理员组

文章数量: 1087649

python 3d图

首先在进行 3D Plot 时除了导入 matplotlib ,还要额外添加一个模块,即 Axes 3D 3D 坐标轴显示:
之后要先定义一个图像窗口,在窗口上添加3D坐标轴,显示成下图:

import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()ax = Axes3D(fig)

接下来给进 X 和 Y 值,并将 X 和 Y 编织成栅格。每一个(X, Y)点对应的高度值我们用下面这个函数来计算

X = np.arange(-4, 4, 0.25)Y = np.arange(-4, 4, 0.25)X, Y = np.meshgrid(X, Y)R = np.sqrt(X ** 2 + Y ** 2)# height valueZ = np.sin(R)

画出3d图:rstride 和 cstride 分别代表 row 和 column 的跨度。

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
   Argument      Description============= ================================================*X*, *Y*, *Z* Data values as 2D arrays*rstride*     Array row stride (step size), defaults to 10*cstride*     Array column stride (step size), defaults to 10*color*       Color of the surface patches*cmap*        A colormap for the surface patches.*facecolors*  Face colors for the individual patches*norm*        An instance of Normalize to map values to colors*vmin*        Minimum value to map*vmax*        Maximum value to map*shade*       Whether to shade the facecolors

若改为:

ax.plot_surface(X, Y, Z, rstride=5, cstride=5, cmap=plt.get_cmap('rainbow'))

  • 画在xy平面的投影
    添加xy等高线的
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.get_cmap('rainbow')

Argument Description

    ==========  ================================================*X*, *Y*,   Data values as numpy.arrays*Z**zdir*      The direction to use: x, y or z (default)*offset*    If specified plot a projection of the filled contouron this position in plane normal to zdir

本文标签: python 3d图