簡體   English   中英

使用 Python3 和 Matplotlib 創建 animation

[英]Create an animation with Python3 and Matplotlib

我嘗試使用matplotlib.pyplotmatplotlib.animation創建一個 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' (我重試了,但沒有收到任何錯誤...

  • 第二,當我取消注釋ani.save('dynamic_images.mp4')我得到這個錯誤: TypeError: 'MovieWriterRegistry' object is not an iterator。 這個更讓我困擾。 如果您對最后一個問題有任何解決方案,請現在告訴我。

狼幫1710。

正如您可以在Animation.save方法的文檔中閱讀的那樣,默認編寫器是“ffmpeg”。 因此,您需要安裝 ffmpeg 才能正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM