1"""
2=================================================
3Animated image using a precomputed list of images
4=================================================
5
6"""
7
8import numpy as np
9import matplotlib.pyplot as plt
10import matplotlib.animation as animation
11
12fig = plt.figure()
13
14
15def f(x, y):
16    return np.sin(x) + np.cos(y)
17
18x = np.linspace(0, 2 * np.pi, 120)
19y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
20# ims is a list of lists, each row is a list of artists to draw in the
21# current frame; here we are just animating one artist, the image, in
22# each frame
23ims = []
24for i in range(60):
25    x += np.pi / 15.
26    y += np.pi / 20.
27    im = plt.imshow(f(x, y), animated=True)
28    ims.append([im])
29
30ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
31                                repeat_delay=1000)
32
33# To save the animation, use e.g.
34#
35# ani.save("movie.mp4")
36#
37# or
38#
39# from matplotlib.animation import FFMpegWriter
40# writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
41# ani.save("movie.mp4", writer=writer)
42
43plt.show()
44