Created
November 29, 2019 16:00
-
-
Save danielmk/c09e8dfd54651cd94e75be15a2038e0e to your computer and use it in GitHub Desktop.
How to make animations with Matplotlib
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 numpy as np | |
from matplotlib.animation import FuncAnimation | |
import matplotlib.pyplot as plt | |
x = np.arange(0, 10*np.pi, 0.01) | |
y = np.sin(x) | |
fig = plt.figure() | |
ax = plt.subplot(1, 1, 1) | |
data_skip = 50 | |
def init_func(): | |
ax.clear() | |
plt.xlabel('pi') | |
plt.ylabel('sin(pi)') | |
plt.xlim((x[0], x[-1])) | |
plt.ylim((-1, 1)) | |
def update_plot(i): | |
ax.plot(x[i:i+data_skip], y[i:i+data_skip], color='k') | |
ax.scatter(x[i], y[i], marker='o', color='r') | |
anim = FuncAnimation(fig, | |
update_plot, | |
frames=np.arange(0, len(x), data_skip), | |
init_func=init_func, | |
interval=20) | |
anim.save('sine.mp4', dpi=150, fps = 30, writer='ffmpeg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment