阻止gif图像在matplotlib中重复 [英] Stop gif image from repeating in matplotlib

查看:81
本文介绍了阻止gif图像在matplotlib中重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了其他类似的问题,但是并不能解决我的问题.下面是一个简单的代码,可在matplotlib中以gif图像的形式生成动画:

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')

这会生成我想要的动画,但是一旦我打开了最终图像(带有eog),它就会不断重复.由于我将在演示文稿中演示动画,因此我希望动画在显示一次后停止播放.如您所见,我在FuncAnimation中添加了 repeat = False ,但这不会停止重复图像.我的代码有什么问题?

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?

预先感谢

推荐答案

我对这个问题的解决方案是从imagemagick变成枕头作家.

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

尝试以下操作:

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))

在我看来,图像质量比使用imagemagick差一些,但这是一个更容易,更快捷的解决方案.

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

这篇关于阻止gif图像在matplotlib中重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆