Skip to content
Advertisement

The saved animated plot keeps looping although Matplotlib FuncAnimation(…,repeat=False)

I’d like to make an animation using matplotlib for a powerpoint presentation. The animation should only play once.

In my code, the argument repeat of FuncAnimation() was set to false. Because I need to import the figure into powerpoint, I saved it using ani.save('test.gif'). The problem is when I open the saved figure which is test.gif, the lineplot keeps looping.

All the searches on Stackoverflow suggest repeat=False, or using PillowWriter. Both solutions are not working for me, so I have two questions:

  1. why does the saved figure ignores repeat=False?
  2. how can I solve this issue?

Thank you in advance for helping me out. Please, find my code below:

JavaScript

Advertisement

Answer

Use ImageMagickWriter:

JavaScript

I spent a lot of time looking into this. Its a little bit of a hack as ‘-loop 0’ is passed to the movie writer (convert) and the extra_args are appended but override. Here is a GIF I created using extra_args=[‘-loop’, ‘1’]:

Loop once GIF NB: Open image in a new tab to see it play.

I never got PillowWriter to work even when overriding the hard coded loop=0 in the finish() method:

JavaScript

Changing the loop parameter from 0 to 1 stops the infinite looping but it still runs twice!

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement