Matplotlib无法保存动画

问题描述 投票:0回答:1

我有一个matplotlib动画,它不会保存。如果我不保存它,它将运行完全正常且没有错误。当我尝试保存该错误时,出现一条无用的消息。我已经用谷歌搜索了这个错误并检查了所有内容,但似乎找不到解决此问题的答案。我已经安装了ffmpeg。我在做明显的错误吗?如果这很重要,我将在带有matplotlib 3.2.1的ubuntu 19.10上运行。

保存动画的代码如下:

    def run_animation(self, total_rounds):
        anim = animation.FuncAnimation(self.fig, self.animate,
                                       init_func=self.init,
                                       frames=total_rounds * 100,
                                       interval=40,
                                       blit=True)
#        Writer = animation.writers['ffmpeg']
#        writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
        anim.save('animation.mp4')

错误回溯:

2020-04-01 02:20:58,279-INFO: MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 1200x500 -pix_fmt rgba -r 25.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y animation.mp4
Traceback (most recent call last):
  File "/home/anon/.local/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2785, in _wait_cursor_for_draw_cm
    self.set_cursor(cursors.WAIT)
  File "/home/anon/.local/lib/python3.7/site-packages/matplotlib/backends/backend_gtk3.py", line 468, in set_cursor
    self.canvas.get_property("window").set_cursor(cursord[cursor])
AttributeError: 'NoneType' object has no attribute 'set_cursor'

非常感谢您的帮助

python matplotlib animation ffmpeg gtk3
1个回答
1
投票

我想出来了,很奇怪,我需要在所有导入语句之前执行此操作。

import matplotlib
matplotlib.use("Agg")

如果我没有那个,那是行不通的。另外,ffmpeg开始需要一段时间,因此我将保存功能修改为:

anim.save('animation.mp4', progress_callback=lambda i, n: print(f'Saving frame {i} of {n}'))

文档中的一个不错的隐藏功能。希望没有人遇到这个问题!

© www.soinside.com 2019 - 2024. All rights reserved.