python 中使用赛璐珞的动画

Animations in python using celluloid

我正在尝试 this 页面中的第一个简单动画。我显然是制作动画的初学者。我粘贴下面的代码。

from matplotlib import pyplot as plt
from celluloid import Camera

fig = plt.figure()
camera = Camera(fig)
for i in range(10):
    plt.plot([i] * 10)
    camera.snap()
animation = camera.animate()

它给我以下错误。

Animation was deleted without rendering anything. This is most likely unintended. To prevent deletion, assign the Animation to a variable that exists for as long as you need the Animation.

据我所知,animate() 已经被赋予了名称。谁能帮我解决这个问题?

要避免这个错误(实际上只是一个UserWarning),你必须显示或保存动画。在您的代码底部:

from matplotlib import pyplot as plt
from celluloid import Camera

fig = plt.figure()
camera = Camera(fig)
for i in range(10):
    plt.plot([i] * 10)
    camera.snap()
animation = camera.animate()

plt.show()
# OR
animation.save('test.mp4')