【问题标题】:Create an animation with Python3 and Matplotlib使用 Python3 和 Matplotlib 创建动画
【发布时间】:2020-04-11 20:20:50
【问题描述】:

我尝试使用matplotlib.pyplotmatplotlib.animation 创建动画,但遇到了两个问题:

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

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(60):
    x += np.pi / 15.
    y += np.pi / 20.
    im = plt.imshow(f(x, y), animated=True)
    ims.append([im])

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

# ani.save('dynamic_images.mp4')

plt.show()

我得到一个错误:AttributeError: 'function' has no attribute 'canvas' (我重新尝试了,我没有得到任何错误......

  • 2nd,当我取消注释 ani.save('dynamic_images.mp4') 时,我收到此错误:TypeError: 'MovieWriterRegistry' object is not an iterator。这个更让我困扰。如果您对最后一个问题有任何解决方案,请现在告诉我。

WolfGang1710.

【问题讨论】:

  • 请重复介绍导览中的MRE

标签: python python-3.x matplotlib animation


【解决方案1】:

正如您在 Animation.save 方法的文档中所读到的,默认编写器是“ffmpeg”。因此,您需要安装 ffmpeg 才能正常工作。

【讨论】:

  • 我这样做了,但不幸的是问题仍然存在。还是谢谢你!
猜你喜欢
  • 2020-08-26
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
  • 2014-02-17
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多