Last active
March 10, 2019 12:40
-
-
Save mtesseracted/79e2f6de43a0dddc48b18c8602735de0 to your computer and use it in GitHub Desktop.
NumPy logo in rotating voxels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
matplotlib must be developer release for voxel support | |
install instructions: | |
https://matplotlib.org/devdocs/users/installing.html | |
''' | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from mpl_toolkits.mplot3d import Axes3D | |
import matplotlib.animation as manimation | |
FFMpegWriter = manimation.writers['ffmpeg'] | |
metadata = dict(title='Movie Test', artist='Matplotlib', | |
comment='Movie support!') | |
writer = FFMpegWriter(fps=25, metadata=metadata) | |
def explode(data): | |
size = np.array(data.shape)*2 | |
data_e = np.zeros(size - 1, dtype=data.dtype) | |
data_e[::2, ::2, ::2] = data | |
return data_e | |
# build up the numpy logo | |
n_voxels = np.zeros((5, 5, 5), dtype=bool) | |
#N | |
n_voxels[0, 0, :] = True | |
n_voxels[-1, 0, :] = True | |
n_voxels[1, 0, 3] = True | |
n_voxels[2, 0, 2] = True | |
n_voxels[3, 0, 1] = True | |
#U | |
n_voxels[0, :, 0] = True | |
n_voxels[-1, :, 0] = True | |
n_voxels[:, -1, 0] = True | |
#Y | |
n_voxels[0, 1, -1] = True | |
n_voxels[-1, 1, -1] = True | |
n_voxels[:, 2, -1] = True | |
n_voxels[2, 3, -1] = True | |
n_voxels[2, 4, -1] = True | |
#M | |
n_voxels[0, -1, :] = True | |
n_voxels[2, -1, :] = True | |
n_voxels[-1, -1, :] = True | |
#P (negative space) | |
n_voxels[-1, 2, 2] = True | |
facecolors = np.where(n_voxels, '#FFD65DC0', '#7A88CC90') | |
#these are rgba values, the last 2 hex digits are opacity, ff=opaque, 00=clear | |
edgecolors = np.where(n_voxels, '#BFAB6E', '#7D84A6') | |
filled = np.ones(n_voxels.shape) | |
# upscale the above voxel image, leaving gaps | |
filled_2 = explode(filled) | |
fcolors_2 = explode(facecolors) | |
ecolors_2 = explode(edgecolors) | |
# Shrink the gaps | |
x, y, z = np.indices(np.array(filled_2.shape) + 1).astype(float) // 2 | |
x[0::2, :, :] += 0.05 | |
y[:, 0::2, :] += 0.05 | |
z[:, :, 0::2] += 0.05 | |
x[1::2, :, :] += 0.95 | |
y[:, 1::2, :] += 0.95 | |
z[:, :, 1::2] += 0.95 | |
fig = plt.figure() | |
ax = fig.gca(projection='3d') | |
ax.axis("off") | |
ax.voxels(x, y, z, filled_2, facecolors=fcolors_2, edgecolors=ecolors_2) | |
view_elev = [-90, -90, -90, -180, 90, -90] | |
view_azim = [0, -90, 180, 180, 90, 0] | |
with writer.saving(fig, "npLogoRotate.mp4", 100): | |
for i in range(1,len(view_elev)): | |
de = (view_elev[i] - view_elev[i-1]) | |
da = (view_azim[i] - view_azim[i-1]) | |
if abs(da) > 180 : | |
if da < 0 : | |
da = da + 360 | |
else : | |
da = da - 360 | |
if abs(de) > 180 : | |
if de < 0 : | |
de = de + 360 | |
else : | |
de = de - 360 | |
da = int(da / 90) | |
de = int(de / 90) | |
for j in range(90): | |
ce = view_elev[i-1] + j*de | |
ca = view_azim[i-1] + j*da | |
ax.view_init(ca, ce) | |
plt.draw() | |
writer.grab_frame() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output