问题
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