Created
February 9, 2021 08:11
-
-
Save snsinfu/52e84893133f22075811dcd55bdd8511 to your computer and use it in GitHub Desktop.
FuncAnimation that adds lines on each frame. No removal.
This file contains hidden or 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
import matplotlib.pyplot as plt | |
import numpy as np | |
from matplotlib.animation import FuncAnimation | |
from IPython.display import Video | |
# Make a spiral | |
step_count = 10000 | |
random = np.random.RandomState(0) | |
k, phi = np.cumsum(random.uniform(0, 0.5, size=(step_count, 2)), axis=0).T | |
x = (k ** 0.7) * np.cos(phi) | |
y = (k ** 0.7) * np.sin(phi) | |
path = np.transpose([x, y]) | |
# Render animation | |
frame_step = 10 | |
frame_interval_milliseconds = 10 | |
video_bitrate_kbps = 512 | |
video_filename = "_a.mp4" | |
def plot_fragment(ax, fragment): | |
ax.plot(fragment[:, 0], fragment[:, 1], lw=0.1, color="red") | |
fig, ax = plt.subplots(figsize=(2, 2), dpi=200) | |
ax.set_xlim(-250, 250) | |
ax.set_ylim(-250, 250) | |
ax.set_xticklabels([]) | |
ax.set_yticklabels([]) | |
ax.set_aspect("equal") | |
frame_count = step_count // frame_step | |
def draw_frame(frame): | |
start = frame * frame_step | |
end = (frame + 1) * frame_step + 1 | |
fragment = path[start:end] | |
plot_fragment(ax, fragment) | |
anim = FuncAnimation( | |
fig, | |
draw_frame, | |
frames=range(frame_count), | |
interval=frame_interval_milliseconds, | |
) | |
anim.save(video_filename, bitrate=video_bitrate_kbps) | |
plt.close(fig) | |
Video(video_filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment