Skip to content

Instantly share code, notes, and snippets.

@danielmk
Created November 29, 2019 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmk/c09e8dfd54651cd94e75be15a2038e0e to your computer and use it in GitHub Desktop.
Save danielmk/c09e8dfd54651cd94e75be15a2038e0e to your computer and use it in GitHub Desktop.
How to make animations with Matplotlib
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