Skip to content

Instantly share code, notes, and snippets.

@finloop
Last active September 5, 2021 18:54
Show Gist options
  • Save finloop/eb1ea56b4aa4967be72db84b17f58915 to your computer and use it in GitHub Desktop.
Save finloop/eb1ea56b4aa4967be72db84b17f58915 to your computer and use it in GitHub Desktop.
Create matplotlib animations and save to mp4
import matplotlib.pylab as plt
from matplotlib import animation
FPS = 12
FRAMES = range(30,90) # iterator for animate function
FILENAME = 'basic_animation.mp4'
# Create fig and axes
fig, ax = plt.subplots(figsize=(20,10))
def animate(i):
ax.clear() # Clear ax (use if each frame is redrawn from scratch)
# Draw plot
# ax.plot ...
# ax.plot ...
# Render video
anim = animation.FuncAnimation(fig, animate, frames=FRAMES , interval=20)
writervideo = animation.FFMpegWriter(fps=FPS)
anim.save(FILENAME, writer=writervideo)
plt.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment