带有rgb颜色的3D体素/体积图

演示使用ax.voxels可视化颜色空间的各个部分

带有rgb颜色的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. def midpoints(x):
  6. sl = ()
  7. for i in range(x.ndim):
  8. x = (x[sl + np.index_exp[:-1]] + x[sl + np.index_exp[1:]]) / 2.0
  9. sl += np.index_exp[:]
  10. return x
  11. # prepare some coordinates, and attach rgb values to each
  12. r, g, b = np.indices((17, 17, 17)) / 16.0
  13. rc = midpoints(r)
  14. gc = midpoints(g)
  15. bc = midpoints(b)
  16. # define a sphere about [0.5, 0.5, 0.5]
  17. sphere = (rc - 0.5)**2 + (gc - 0.5)**2 + (bc - 0.5)**2 < 0.5**2
  18. # combine the color components
  19. colors = np.zeros(sphere.shape + (3,))
  20. colors[..., 0] = rc
  21. colors[..., 1] = gc
  22. colors[..., 2] = bc
  23. # and plot everything
  24. fig = plt.figure()
  25. ax = fig.gca(projection='3d')
  26. ax.voxels(r, g, b, sphere,
  27. facecolors=colors,
  28. edgecolors=np.clip(2*colors - 0.5, 0, 1), # brighter
  29. linewidth=0.5)
  30. ax.set(xlabel='r', ylabel='g', zlabel='b')
  31. plt.show()

下载这个示例