|
| 1 | +''' |
| 2 | +=============================== |
| 3 | +3D voxel plot of the numpy logo |
| 4 | +=============================== |
| 5 | +
|
| 6 | +Demonstrates using ``ax.voxels`` with uneven coordinates |
| 7 | +''' |
| 8 | +import matplotlib.pyplot as plt |
| 9 | +import numpy as np |
| 10 | +from mpl_toolkits.mplot3d import Axes3D |
| 11 | + |
| 12 | + |
| 13 | +def explode(data): |
| 14 | + size = np.array(data.shape)*2 |
| 15 | + data_e = np.zeros(size - 1, dtype=data.dtype) |
| 16 | + data_e[::2, ::2, ::2] = data |
| 17 | + return data_e |
| 18 | + |
| 19 | +# build up the numpy logo |
| 20 | +n_voxels = np.zeros((4, 3, 4), dtype=bool) |
| 21 | +n_voxels[0, 0, :] = True |
| 22 | +n_voxels[-1, 0, :] = True |
| 23 | +n_voxels[1, 0, 2] = True |
| 24 | +n_voxels[2, 0, 1] = True |
| 25 | +facecolors = np.where(n_voxels, '#FFD65DC0', '#7A88CCC0') |
| 26 | +edgecolors = np.where(n_voxels, '#BFAB6E', '#7D84A6') |
| 27 | +filled = np.ones(n_voxels.shape) |
| 28 | + |
| 29 | +# upscale the above voxel image, leaving gaps |
| 30 | +filled_2 = explode(filled) |
| 31 | +fcolors_2 = explode(facecolors) |
| 32 | +ecolors_2 = explode(edgecolors) |
| 33 | + |
| 34 | +# Shrink the gaps |
| 35 | +x, y, z = np.indices(np.array(filled_2.shape) + 1).astype(float) // 2 |
| 36 | +x[0::2, :, :] += 0.05 |
| 37 | +y[:, 0::2, :] += 0.05 |
| 38 | +z[:, :, 0::2] += 0.05 |
| 39 | +x[1::2, :, :] += 0.95 |
| 40 | +y[:, 1::2, :] += 0.95 |
| 41 | +z[:, :, 1::2] += 0.95 |
| 42 | + |
| 43 | +fig = plt.figure() |
| 44 | +ax = fig.gca(projection='3d') |
| 45 | +ax.voxels(x, y, z, filled_2, facecolors=fcolors_2, edgecolors=ecolors_2) |
| 46 | + |
| 47 | +plt.show() |
0 commit comments