尽管 Matplotlib FuncAnimation(...,repeat=False) 保存的动画图不断循环

Ami*_*sky 4 python animation matplotlib

我想制作一个用于matplotlib幻灯片演示的动画。动画应该只播放一次。

在我的代码中, 的参数repeat设置FuncAnimation()为 false。因为我需要将图导入到powerpoint中,所以我使用ani.save('test.gif'). 问题是当我打开保存的图形 test.gif 时,线图不断循环。

Stackoverflow 上的所有搜索都建议repeat=False使用PillowWriter。这两种解决方案都不适合我,所以我有两个问题:

  1. 为什么保存的数字会被忽略repeat=False
  2. 我该如何解决这个问题?

预先感谢您帮助我。请在下面找到我的代码:

import pandas as pd

import matplotlib.pyplot as plt

# import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation, PillowWriter

df = pd.DataFrame(
    {
        "x": {
            0: 0,
            1: 1,
            2: 2,
            3: 3,
            4: 4,
            5: 5,
            6: 6,
            7: 7,
            8: 8,
            9: 9,
            10: 10,
            11: 11,
            12: 12,
            13: 13,
            14: 14,
            15: 15,
            16: 16,
            17: 17,
            18: 18,
            19: 19,
            20: 20,
            21: 21,
            22: 22,
            23: 23,
            24: 24,
            25: 25,
            26: 26,
            27: 27,
            28: 28,
            29: 29,
            30: 30,
            31: 31,
            32: 32,
            33: 33,
            34: 34,
            35: 35,
            36: 36,
            37: 37,
            38: 38,
            39: 39,
            40: 40,
            41: 41,
            42: 42,
            43: 43,
            44: 44,
            45: 45,
            46: 46,
            47: 47,
            48: 48,
            49: 49,
            50: 50,
        },
        "y": {
            0: 0.7695,
            1: 0.7983,
            2: 0.7958,
            3: 0.7975,
            4: 0.7983,
            5: 0.7966,
            6: 0.7971,
            7: 0.7962,
            8: 0.7962,
            9: 0.7975,
            10: 0.7983,
            11: 0.7987,
            12: 0.7996,
            13: 0.7992,
            14: 0.7967,
            15: 0.7983,
            16: 0.7971,
            17: 0.7987,
            18: 0.7979,
            19: 0.7983,
            20: 0.7983,
            21: 0.7921,
            22: 0.7975,
            23: 0.7962,
            24: 0.7975,
            25: 0.7979,
            26: 0.7983,
            27: 0.7992,
            28: 0.7983,
            29: 0.7983,
            30: 0.7987,
            31: 0.7983,
            32: 0.7983,
            33: 0.7983,
            34: 0.7992,
            35: 0.7975,
            36: 0.7996,
            37: 0.7992,
            38: 0.7979,
            39: 0.7987,
            40: 0.7983,
            41: 0.7983,
            42: 0.7987,
            43: 0.7987,
            44: 0.7992,
            45: 0.7992,
            46: 0.7979,
            47: 0.7996,
            48: 0.7992,
            49: 0.7987,
            50: 0.7992,
        },
    }
)


x = df["x"]
y = df["y"]
fig, ax = plt.subplots()
line, = ax.plot(x, y, color="b")

def update(num, x, y, line):
    line.set_data(x[:num], y[:num])
    return (line,)

ani = FuncAnimation(
    fig, update, len(x), fargs=[x, y, line], interval=15, blit=True, repeat=False
)

# ani.save('test.gif')
ani.save("test2.gif", dpi=80, writer=PillowWriter(fps=5))

plt.show()
Run Code Online (Sandbox Code Playgroud)

Sha*_*dew 5

使用ImageMagickWriter:

ani.save("test2.gif", dpi=80, writer=ImageMagickWriter(fps=5, extra_args=['-loop', '1']))
Run Code Online (Sandbox Code Playgroud)

我花了很多时间研究这个。它有点像黑客,因为“-loop 0”被传递给电影编写器(转换),并且附加了 extra_args 但被覆盖。这是我使用 extra_args=['-loop', '1'] 创建的 GIF:

循环一次 GIF 注意:在新选项卡中打开图像以查看其播放。

即使在finish() 方法中覆盖硬编码的 Loop=0 时,我也从未让 PillowWriter 工作:

class PillowWriterNG(PillowWriter):
    def finish(self):
            self._frames[0].save(
                self.outfile, save_all=True, append_images=self._frames[1:],
                duration=int(1000 / self.fps), loop=1)
Run Code Online (Sandbox Code Playgroud)

将循环参数从 0 更改为 1 可停止无限循环,但它仍然运行两次!