Matplotlib 如何利用子图制作gif?

Matplotlib 如何利用子图制作gif?,matplotlib,Matplotlib,我使用matplotlib网站上的代码通过图像列表生成gif。 然而,如果我有两个轴在里面的子图,我正在努力找出如何使它工作。因此,就好像我有两个图像,我应该在列表中附加哪一个 编辑:示例代码: 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 = animatio

我使用matplotlib网站上的代码通过图像列表生成gif。

然而,如果我有两个轴在里面的子图,我正在努力找出如何使它工作。因此,就好像我有两个图像,我应该在列表中附加哪一个

编辑:示例代码:

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)
如中所述,传递给
ArtistAnimation
的艺术家数组是一个列表列表,列表中的每个元素对应一个帧,“内部”列表中的所有元素都会在该帧中更新

所以

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

fig, (ax1, ax2) = plt.subplots(1,2)


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.
    im1 = ax1.imshow(f(x, y), animated=True)
    im2 = ax2.imshow(np.random.random(size=(100,120)))
    ims.append([im1,im2])

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