三维体素/体积绘制

演示使用ax.voxels绘制3D体积对象

三维体素/体积绘制示例

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # This import registers the 3D projection, but is otherwise unused.
  4. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
  5. # prepare some coordinates
  6. x, y, z = np.indices((8, 8, 8))
  7. # draw cuboids in the top left and bottom right corners, and a link between them
  8. cube1 = (x < 3) & (y < 3) & (z < 3)
  9. cube2 = (x >= 5) & (y >= 5) & (z >= 5)
  10. link = abs(x - y) + abs(y - z) + abs(z - x) <= 2
  11. # combine the objects into a single boolean array
  12. voxels = cube1 | cube2 | link
  13. # set the colors of each object
  14. colors = np.empty(voxels.shape, dtype=object)
  15. colors[link] = 'red'
  16. colors[cube1] = 'blue'
  17. colors[cube2] = 'green'
  18. # and plot everything
  19. fig = plt.figure()
  20. ax = fig.gca(projection='3d')
  21. ax.voxels(voxels, facecolors=colors, edgecolor='k')
  22. plt.show()

下载这个示例