Skip to content

Instantly share code, notes, and snippets.

@andiac
Created February 10, 2022 22:51
Show Gist options
  • Save andiac/7d4a775a907b396aa1cf22f28eb9b157 to your computer and use it in GitHub Desktop.
Save andiac/7d4a775a907b396aa1cf22f28eb9b157 to your computer and use it in GitHub Desktop.
Matplotlib patch animation
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()
@andiac
Copy link
Author

andiac commented Feb 10, 2022

motion.mp4

@andiac
Copy link
Author

andiac commented Feb 23, 2022

ho.mp4

@andiac
Copy link
Author

andiac commented Feb 23, 2022

ver.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment