Last active
September 5, 2021 18:54
-
-
Save finloop/eb1ea56b4aa4967be72db84b17f58915 to your computer and use it in GitHub Desktop.
Create matplotlib animations and save to mp4
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
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