Created
May 3, 2018 16:14
-
-
Save akey7/740bdcad7bf8f7028e9efff4a8e74be1 to your computer and use it in GitHub Desktop.
PY: Animated GIF for sine wave
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
# See for tiao.io/posts/notebooks/save-matplotlib-animations-as-gifs/ more information and notes below | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib import animation, rc | |
from IPython.display import HTML, Image | |
# equivalent to rcParams['animation.html'] = 'html5' | |
rc('animation', html='html5') | |
fig, ax = plt.subplots() | |
ax.set_xlim(( 0, 2)) | |
ax.set_ylim((-2, 2)) | |
line, = ax.plot([], [], lw=2) | |
def init(): | |
line.set_data([], []) | |
return (line,) | |
def animate(i): | |
x = np.linspace(0, 2, 1000) | |
y = np.sin(2 * np.pi * (x - 0.01 * i)) | |
line.set_data(x, y) | |
return (line,) | |
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=20, blit=True) | |
anim.save('animation.gif', writer='imagemagick', fps=60) | |
# You will have to install imagemagick with homebrew "brew install imagemagick" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment