Stop gif image from repeating in matplotlib

て烟熏妆下的殇ゞ 提交于 2021-01-20 07:07:09

问题


I searched for other similar questions but that didn't solve my problem. Below is a simple code that generates an animation in the form of a gif image in matplotlib:

import numpy as np
import matplotlib.pylab as plt
import matplotlib.animation as anm

fig = plt.figure()
def draw(i):
    x = np.linspace(0, 5, num = 1000)    
    y = np.sin(x-i*0.1)
    plt.clf()
    plt.plot(x, y, 'r-') 

anim = anm.FuncAnimation(fig, draw, frames = 10, interval = 500, repeat = False)
anim.save('test.gif', fps = 1, writer = 'imagemagick')

This generates the animation I want but once I open the final image (with eog), it keeps repeating. Since I would be presenting animation in the presentation, I would like it to stop after it is shown once. As you can notice, I have added repeat = False in the FuncAnimation but that doesn't stop repeating the image. What is wrong with my code?

Thanks in advance


回答1:


My solution to the problem was to change from imagemagick to pillow as a writer.

Try the following:

from matplotlib.animation import FuncAnimation, PillowWriter

anim = FuncAnimation(fig, update, frames=range(1, 51), interval=.001, repeat=False)
anim.save('test.gif', dpi=80, writer=PillowWriter(fps=5))

The quality of the image is a bit worse than with imagemagick from my opinion, but it's a much easier and faster solution.



来源:https://stackoverflow.com/questions/35111351/stop-gif-image-from-repeating-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!