Last active
October 21, 2019 06:21
-
-
Save sabopy/09f9d3fe42c2b34bcbca95838b5e64be to your computer and use it in GitHub Desktop.
mplot3d wireplot animation
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
#ArtistAnimation | |
from mpl_toolkits.mplot3d import Axes3D | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from IPython.display import HTML | |
import matplotlib.animation as animation | |
def generate(X, Y, phi): | |
R = 1 - np.sqrt(X**2 + Y**2) | |
return np.cos(2 * np.pi * X + phi) * R | |
fig = plt.figure() | |
ax = fig.add_subplot(111, projection='3d') | |
# Make the X, Y meshgrid. | |
xs = np.linspace(-1, 1, 50) | |
ys = np.linspace(-1, 1, 50) | |
X, Y = np.meshgrid(xs, ys) | |
# Set the z axis limits so they aren't recalculated each frame. | |
ax.set_zlim(-1, 1) | |
#animation function | |
phi = np.linspace(0, 180. / np.pi, 50) | |
ims = [] | |
for i in range(50): | |
at_z = phi[i] | |
Z = generate(X,Y,at_z) | |
im = ax.plot_wireframe(X, Y, Z, color='g',rstride=2, cstride=2) | |
ims.append([im]) | |
ani = animation.ArtistAnimation(fig, ims, interval=100) | |
ani.save('wf_anim_art.mp4',writer='ffmpeg',dpi=100) | |
HTML(ani.to_html5_video()) | |
#FuncAnimation | |
fig = plt.figure() | |
ax = fig.add_subplot(111, projection='3d') | |
ax.set_zlim(-1, 1) | |
def generate(X, Y, phi): | |
R = 1 - np.sqrt(X**2 + Y**2) | |
return np.cos(2 * np.pi * X + phi) * R | |
# Make the X, Y meshgrid. | |
xs = np.linspace(-1, 1, 50) | |
ys = np.linspace(-1, 1, 50) | |
X, Y = np.meshgrid(xs, ys) | |
phi = np.linspace(0, 180. / np.pi, 50) | |
Z_list=[] | |
for i in range(50): | |
Z = generate(X,Y,phi[i]) | |
Z_list.append(Z) | |
wf = [] | |
def update(num): | |
if len(wf) > 0: | |
wf.pop().remove() | |
wf_, = [ax.plot_wireframe(X, Y, Z_list[num], color='g',rstride=2, cstride=2)] | |
wf.append(wf_) | |
ani = animation.FuncAnimation(fig, update, 50,interval=100) | |
ani.save('wf_anim_func.mp4',writer='ffmpeg',dpi=100) | |
HTML(ani.to_html5_video()) | |
%%time | |
fig = plt.figure() | |
ax = fig.add_subplot(111, projection='3d') | |
# Make the X, Y meshgrid. | |
xs = np.linspace(-1, 1, 50) | |
ys = np.linspace(-1, 1, 50) | |
X, Y = np.meshgrid(xs, ys) | |
# Set the z axis limits so they aren't recalculated each frame. | |
ax.set_zlim(-1, 1) | |
#animation function | |
phi = np.linspace(0, 180. / np.pi, 50) | |
ims = [] | |
for i in range(50): | |
at_z = phi[i] | |
Z = generate(X,Y,at_z) | |
im = ax.plot_wireframe(X, Y, Z, color='g',rstride=2, cstride=2) | |
ims.append([im]) | |
ani = animation.ArtistAnimation(fig, ims, interval=100) | |
HTML(ani.to_html5_video()) | |
''' | |
CPU times: user 1min 25s, sys: 526 ms, total: 1min 26s | |
Wall time: 1min 26s | |
''' | |
%%time | |
fig = plt.figure() | |
ax = fig.add_subplot(111, projection='3d') | |
ax.set_zlim(-1, 1) | |
def generate(X, Y, phi): | |
R = 1 - np.sqrt(X**2 + Y**2) | |
return np.cos(2 * np.pi * X + phi) * R | |
# Make the X, Y meshgrid. | |
xs = np.linspace(-1, 1, 50) | |
ys = np.linspace(-1, 1, 50) | |
X, Y = np.meshgrid(xs, ys) | |
phi = np.linspace(0, 180. / np.pi, 50) | |
Z_list=[] | |
for i in range(50): | |
Z = generate(X,Y,phi[i]) | |
Z_list.append(Z) | |
wf = [] | |
def update(num): | |
if len(wf) > 0: | |
wf.pop().remove() | |
wf_, = [ax.plot_wireframe(X, Y, Z_list[num], color='g',rstride=2, cstride=2)] | |
wf.append(wf_) | |
ani = animation.FuncAnimation(fig, update, 50,interval=100) | |
HTML(ani.to_html5_video()) | |
''' | |
CPU times: user 16.7 s, sys: 446 ms, total: 17.1 s | |
Wall time: 17.6 s | |
''' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment