具有圆柱坐标的3D体素/体积图

演示使用ax.voxels的x,y,z参数。

具有圆柱坐标的3D体素/体积图示例

  1. import matplotlib.pyplot as plt
  2. import matplotlib.colors
  3. import numpy as np
  4. # This import registers the 3D projection, but is otherwise unused.
  5. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
  6. def midpoints(x):
  7. sl = ()
  8. for i in range(x.ndim):
  9. x = (x[sl + np.index_exp[:-1]] + x[sl + np.index_exp[1:]]) / 2.0
  10. sl += np.index_exp[:]
  11. return x
  12. # prepare some coordinates, and attach rgb values to each
  13. r, theta, z = np.mgrid[0:1:11j, 0:np.pi*2:25j, -0.5:0.5:11j]
  14. x = r*np.cos(theta)
  15. y = r*np.sin(theta)
  16. rc, thetac, zc = midpoints(r), midpoints(theta), midpoints(z)
  17. # define a wobbly torus about [0.7, *, 0]
  18. sphere = (rc - 0.7)**2 + (zc + 0.2*np.cos(thetac*2))**2 < 0.2**2
  19. # combine the color components
  20. hsv = np.zeros(sphere.shape + (3,))
  21. hsv[..., 0] = thetac / (np.pi*2)
  22. hsv[..., 1] = rc
  23. hsv[..., 2] = zc + 0.5
  24. colors = matplotlib.colors.hsv_to_rgb(hsv)
  25. # and plot everything
  26. fig = plt.figure()
  27. ax = fig.gca(projection='3d')
  28. ax.voxels(x, y, z, sphere,
  29. facecolors=colors,
  30. edgecolors=np.clip(2*colors - 0.5, 0, 1), # brighter
  31. linewidth=0.5)
  32. plt.show()

下载这个示例