Python 使用Matplotlib imshow使用ArtistAnimation制作动画时出现异常类型错误

Python 使用Matplotlib imshow使用ArtistAnimation制作动画时出现异常类型错误,python,animation,matplotlib,typeerror,Python,Animation,Matplotlib,Typeerror,我试图使用的示例,以便为从文件中获取的2D数组序列设置动画。为了做到这一点,我需要在函数内部使用ArtistAnimation,但是这个简单的更改给了我一个我不理解的TypeError。我所做的修改是: import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation def f(x, y): return np.sin(x) + np.cos(y) x = n

我试图使用的示例,以便为从文件中获取的2D数组序列设置动画。为了做到这一点,我需要在函数内部使用ArtistAnimation,但是这个简单的更改给了我一个我不理解的TypeError。我所做的修改是:

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




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
def animate(x,y):
    fig = plt.figure()
    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()
我收到的TypeError消息是:

In [23]: animate(x,y)
Exception TypeError: TypeError("'instancemethod' object is not connected",) in <bound method TimerQT.__del__ of <matplotlib.backends.backend_qt5.TimerQT object at 0x7f964ea06150>> ignored
[23]中的
:设置动画(x,y)
异常TypeError:忽略中的TypeError(“instancemethod”对象未连接)
正如上面所说:“保持对实例对象的引用是至关重要的”。
另外,您需要在最后调用
plt.show()

虽然这在作为脚本运行时不是问题,但在jupyter笔记本中,您需要将代码更改为

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


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)

def animate(x,y):
    fig = plt.figure()
    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)

    return ani
然后像这样称呼它

ani = animate(x,y)
plt.show()
保留对动画的引用。

如所述:“保留对实例对象的引用非常重要”。
另外,您需要在最后调用
plt.show()

虽然这在作为脚本运行时不是问题,但在jupyter笔记本中,您需要将代码更改为

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


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)

def animate(x,y):
    fig = plt.figure()
    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)

    return ani
然后像这样称呼它

ani = animate(x,y)
plt.show()

保留对动画的引用。

此处显示的代码几乎没有意义。更改示例的原因是什么?这里显示的代码几乎没有意义。更改示例的原因是什么?