Created
November 29, 2019 16:01
-
-
Save danielmk/f7d2c5f1be1ca9b9b4d1654509c3f990 to your computer and use it in GitHub Desktop.
How to animate two plots 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_sin = np.sin(x) | |
y_cos = np.cos(x) | |
fig = plt.figure() | |
ax1 = plt.subplot(2, 1, 1) | |
ax2 = plt.subplot(2, 1, 2) | |
data_skip = 50 | |
def init_func(): | |
ax1.clear() | |
ax2.clear() | |
ax1.set_xlabel('pi') | |
ax1.set_ylabel('sin(pi)') | |
ax2.set_xlabel('pi') | |
ax2.set_ylabel('cos(pi)') | |
ax1.set_xlim((x[0], x[-1])) | |
ax1.set_ylim((-1, 1)) | |
ax2.set_xlim((x[0], x[-1])) | |
ax2.set_ylim((-1, 1)) | |
def update_plot(i): | |
# ax.clear() | |
ax1.plot(x[i:i+data_skip], y_sin[i:i+data_skip], color='k') | |
ax1.scatter(x[i], y_sin[i], marker='o', color='r') | |
ax2.plot(x[i:i+data_skip], y_cos[i:i+data_skip], color='k') | |
ax2.scatter(x[i], y_cos[i], marker='o', color='r') | |
anim = FuncAnimation(fig, | |
update_plot, | |
frames=np.arange(0, len(x), data_skip), | |
init_func=init_func, | |
interval=1) | |
anim.save('sin_cos.mp4', dpi=150, fps=10, writer='ffmpeg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment