Saving to gif doesn't erase previous plot

Hi,

I’m doing some funny fractals visualization using MatPlotLib. I use an animation to show generations. And the application it self is fine. but gif saved from animation shows all generations. It doesn’t erase previous. The source code is here https://github.com/feliastre/math-samples/blob/main/dragon.py

Is it possible to do gif as fine as animation shown by app itself?

So instead of generating a new plot on every round (plt.plot()), you’re probably better off using the object oriented interface and changing the x,y on the object on every new frame: https://matplotlib.org/gallery/animation/animate_decay.html#sphx-glr-gallery-animation-animate-decay-py

something like

fig, ax = plt.subplots()
line, = ax.plot(frames[0][0], frames[0][1])

def plot(index):
    line.set_data(frames[index][0], frames[index][1])
    return line,

done this, and everything dissappear. )) Seems to be wrong direction to dig into.

Nah, I just didn’t fully work out the example. plt.plot() creates a new artist on every call, and that’s why the previous line wasn’t erased. In the line, _ = ax.plot version (which granted, you can also do line, _ = plt.plot, you have the line plot in the form of the line object. Which you can then overwrite with new data using line.set_data. Here’s a tutorial that will maybe help:

The example works fine! I have to make my source to work same way! Thanks a lot!

1 Like