-
-
Save djvra/edc56dc2f4be4db80acc896605707501 to your computer and use it in GitHub Desktop.
Using matplotlib's FuncAnimation to do a basic animation of a sine wave moving across the screen:
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
| import numpy as np | |
| from matplotlib import pyplot as plt | |
| from matplotlib.animation import FuncAnimation | |
| plt.style.use('seaborn-pastel') | |
| fig = plt.figure() | |
| ax = plt.axes(xlim=(0, 4), ylim=(-2, 2)) | |
| line, = ax.plot([], [], lw=3) | |
| def init(): | |
| line.set_data([], []) | |
| return line, | |
| def animate(i): | |
| x = np.linspace(0, 4, 1000) | |
| y = np.sin(2 * np.pi * (x - 0.01 * i)) | |
| line.set_data(x, y) | |
| return line, | |
| anim = FuncAnimation(fig, animate, init_func=init, | |
| frames=200, interval=20, blit=True) | |
| anim.save('sine_wave.gif', writer='imagemagick') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment