Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a matplotlib animation, by clf()'ing each frame

i am trying to make the simplest matplotlib animation, using animation.FuncAnimation. I dont care about efficiency. i do not want to keep track of the plotted lines and update their data (in my desired application this would be annoying), i simply want to erase the plot before animating every frame. i thought somthing like this should work, but its not..

import matplotlib.animation as animation

fig = Figure()

def updatefig(i):
    clf()
    p = plot(rand(100))
    draw()

anim = animation.FuncAnimation(fig, updatefig, range(10))
like image 447
alex Avatar asked Sep 10 '25 08:09

alex


1 Answers

At least this seems to work:

import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()

def updatefig(i):
    fig.clear()
    p = plt.plot(np.random.random(100))
    plt.draw()

anim = animation.FuncAnimation(fig, updatefig, 10)
anim.save("/tmp/test.mp4", fps=1)

The issue with the original code is Figure written with a capital F (should be figure).

Otherwise, I would suggest not to use the pylab style "everything in the same namespace" approach with matplotlib. Also, using the object-oriented interface instead of plt.draw, plt.plot, etc. will save a lot of trouble later on.

like image 87
DrV Avatar answered Sep 12 '25 21:09

DrV