如何使Matplotlib保存gif循环 [英] How to make Matplotlib saved gif looping

查看:134
本文介绍了如何使Matplotlib保存gif循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:

  • Matplotlib v2.2.2

代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import PillowWriter

fig = plt.figure()

def f(x, y):
    return np.sin(x) + np.cos(y)

x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
# ims is a list of lists, each row is a list of artists to draw in the
# current frame; here we are just animating one artist, the image, in
# each frame
ims = []
for i in range(20):
    x += np.pi / 15.
    y += np.pi / 20.
    im = plt.imshow(f(x, y))
    ims.append([im])

ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
                                repeat_delay=500)

writer = PillowWriter(fps=20)
ani.save("demo2.gif", writer=writer)

plt.show()

输出: 它只能播放一次.

Output: It only play once.

推荐答案

使用imagemagick作为编写器会产生循环的gif,但我不能告诉您为什么这不适用于PillowWriter

Using imagemagick as a writer produces a looping gif, but I can't tell you why that does not work with PillowWriter

ani.save("demo2.gif", writer='imagemagick')

这篇关于如何使Matplotlib保存gif循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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