loops - 如何使Matplotlib保存gif循环

标签 loops 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()
    

    输出:
    它只播放一次。

    enter image description here

    最佳答案

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

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

    enter image description here

    关于loops - 如何使Matplotlib保存gif循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51512141/

    相关文章:

    c++ - 循环内的迭代器不会递增

    python - 如何生成带有截断条形的类似条形图的图?

    Java - GIF 到 base64 字符串

    javascript - 如何在悬停时制作 gif 动画,悬停后恢复正常

    Java - 继续游戏

    java - 猜测循环卡住 Java

    r - 如何根据条件将向量元素与前一个元素粘贴在一起

    python - 使用matplotlib : plot error bars on two y axes

    python - 为什么在通过 Python 的 C API 调用 pyplot.draw() 时重新播种 rand()?

    php - 使用 GIFDecoder.class.php 调整动画 GIF 大小会导致透明度问题