3D绘图作为子图

展示包括3D图作为子图。

3D绘图作为子图示例

  1. import matplotlib.pyplot as plt
  2. from matplotlib import cm
  3. import numpy as np
  4. from mpl_toolkits.mplot3d.axes3d import get_test_data
  5. # This import registers the 3D projection, but is otherwise unused.
  6. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
  7. # set up a figure twice as wide as it is tall
  8. fig = plt.figure(figsize=plt.figaspect(0.5))
  9. #===============
  10. # First subplot
  11. #===============
  12. # set up the axes for the first plot
  13. ax = fig.add_subplot(1, 2, 1, projection='3d')
  14. # plot a 3D surface like in the example mplot3d/surface3d_demo
  15. X = np.arange(-5, 5, 0.25)
  16. Y = np.arange(-5, 5, 0.25)
  17. X, Y = np.meshgrid(X, Y)
  18. R = np.sqrt(X**2 + Y**2)
  19. Z = np.sin(R)
  20. surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
  21. linewidth=0, antialiased=False)
  22. ax.set_zlim(-1.01, 1.01)
  23. fig.colorbar(surf, shrink=0.5, aspect=10)
  24. #===============
  25. # Second subplot
  26. #===============
  27. # set up the axes for the second plot
  28. ax = fig.add_subplot(1, 2, 2, projection='3d')
  29. # plot a 3D wireframe like in the example mplot3d/wire3d_demo
  30. X, Y, Z = get_test_data(0.05)
  31. ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
  32. plt.show()

下载这个示例