Created
February 10, 2022 22:51
-
-
Save andiac/7d4a775a907b396aa1cf22f28eb9b157 to your computer and use it in GitHub Desktop.
Matplotlib patch animation
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 | |
import matplotlib.pyplot as plt | |
from matplotlib import animation | |
from matplotlib.collections import PatchCollection | |
fig = plt.figure() | |
ax = plt.axes(xlim=(-1.5, 1.5), ylim=(-1.5, 1.5), aspect='equal') | |
# not visible | |
circle = matplotlib.patches.Circle((-10.0, -10.0), 0.2, color='blue') | |
collection = PatchCollection([circle]) | |
ax.add_collection(collection) | |
ax.add_patch(circle) | |
def init(): | |
circle.center = (np.sin(0.0), np.cos(0.0)) | |
return collection, | |
def animate(i): | |
circle.center = (np.sin(i/30), np.cos(i/30)) | |
return collection, | |
anim = animation.FuncAnimation(fig, animate, init_func=init, interval=20, blit=True, frames=300) | |
anim.save('motion.mp4', fps=30, dpi=70, extra_args=['-vcodec', 'libx264']) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ho.mp4