Created
July 12, 2021 04:47
-
-
Save jigangkim/235e97b3f8a37a3151799e38858ab192 to your computer and use it in GitHub Desktop.
matplotlib animation examples
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 | |
import matplotlib.pyplot as plt | |
import matplotlib.animation as animation | |
fig = plt.figure() | |
Writer = animation.writers['ffmpeg'] | |
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800) | |
def f(x, y): | |
return np.sin(x) + np.cos(y) | |
x = np.linspace(0, 2 * np.pi, 120) | |
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1) | |
# ims is a list of lists, each row is a list of artists to draw in the | |
# current frame; here we are just animating one artist, the image, in | |
# each frame | |
ims = [] | |
for i in range(60): | |
x += np.pi / 15. | |
y += np.pi / 20. | |
im = plt.imshow(f(x, y), animated=True) | |
ims.append([im]) | |
ani = animation.ArtistAnimation(fig, ims, interval=50) | |
ani.save('dynamic_images(slow).mp4', writer=writer) | |
fig.clear() | |
fig = plt.figure() | |
x = np.linspace(0, 10 * np.pi, 120) | |
y = np.linspace(0, 10 * np.pi, 100).reshape(-1, 1) | |
# ims is a list of lists, each row is a list of artists to draw in the | |
# current frame; here we are just animating one artist, the image, in | |
# each frame | |
ims = [] | |
for i in range(60): | |
x += np.pi / 15. | |
y += np.pi / 20. | |
im = plt.imshow(f(x, y), animated=True) | |
ims.append([im]) | |
ani = animation.ArtistAnimation(fig, ims, interval=50) | |
ani.save('dynamic_images(fast).mp4', writer=writer) | |
fig.clear() | |
# plt.show() |
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 random | |
import time | |
from matplotlib import pyplot as plt | |
from matplotlib import animation | |
class RegrMagic(object): | |
"""Mock for function Regr_magic() | |
""" | |
def __init__(self): | |
self.x = 0 | |
# self.empty_buffer = True | |
def __call__(self): | |
# while self.empty_buffer: | |
# pass # idle when no input | |
time.sleep(random.random()) | |
self.x += 1 | |
# self.empty_buffer = True | |
return self.x, random.random() | |
# def update(self, y): | |
# self.empty_buffer = False | |
# self.y = y | |
regr_magic = RegrMagic() | |
def frames(): | |
while True: | |
yield regr_magic() | |
Writer = animation.writers['ffmpeg'] | |
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800) | |
fig = plt.figure() | |
x = [] | |
y = [] | |
def animate(args): | |
x.append(args[0]) | |
y.append(args[1]) | |
return plt.plot(x, y, color='g') | |
anim = animation.FuncAnimation(fig, animate, frames=frames, interval=1000) | |
# for i in range(10): | |
# regr_magic.update(i) | |
# print('frame updated') | |
# time.sleep(1.0) | |
# anim.save('dynamic_images.mp4', writer=writer) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment