← retour aux snippets

matplotlib: animation FuncAnimation

Animer une ligne simple avec FuncAnimation.

objectif

Animer une ligne simple avec FuncAnimation.

code minimal

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
line, = ax.plot([], [])
def init(): line.set_data([], []); return (line,)
def update(frame): line.set_data([0,frame], [0,frame]); return (line,)
anim = FuncAnimation(fig, update, init_func=init, frames=5, blit=True)
print(hasattr(anim, "to_html5_video"))

utilisation

print("use anim.save('out.mp4', fps=10) en local")

variante(s) utile(s)

print("ok")

notes

  • Nécessite ffmpeg pour sauver en vidéo.