三维体素绘制Numpy的Logo

演示使用坐标不均匀的ax.voxels

三维体素绘制Numpy的Logo示例

  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 explode(data):
  6. size = np.array(data.shape)*2
  7. data_e = np.zeros(size - 1, dtype=data.dtype)
  8. data_e[::2, ::2, ::2] = data
  9. return data_e
  10. # build up the numpy logo
  11. n_voxels = np.zeros((4, 3, 4), dtype=bool)
  12. n_voxels[0, 0, :] = True
  13. n_voxels[-1, 0, :] = True
  14. n_voxels[1, 0, 2] = True
  15. n_voxels[2, 0, 1] = True
  16. facecolors = np.where(n_voxels, '#FFD65DC0', '#7A88CCC0')
  17. edgecolors = np.where(n_voxels, '#BFAB6E', '#7D84A6')
  18. filled = np.ones(n_voxels.shape)
  19. # upscale the above voxel image, leaving gaps
  20. filled_2 = explode(filled)
  21. fcolors_2 = explode(facecolors)
  22. ecolors_2 = explode(edgecolors)
  23. # Shrink the gaps
  24. x, y, z = np.indices(np.array(filled_2.shape) + 1).astype(float) // 2
  25. x[0::2, :, :] += 0.05
  26. y[:, 0::2, :] += 0.05
  27. z[:, :, 0::2] += 0.05
  28. x[1::2, :, :] += 0.95
  29. y[:, 1::2, :] += 0.95
  30. z[:, :, 1::2] += 0.95
  31. fig = plt.figure()
  32. ax = fig.gca(projection='3d')
  33. ax.voxels(x, y, z, filled_2, facecolors=fcolors_2, edgecolors=ecolors_2)
  34. plt.show()

下载这个示例