ArtistAnimation の第2引数に渡す内容は各フレームの Artist の一覧です。
つまり、以下のような2次元のリストを渡す必要があります。
[1フレーム目の Artist のリスト、2フレーム目の Artist のリスト、...]
Artist の意味は以下を参照
早く知っておきたかったmatplotlibの基礎知識、あるいは見た目の調整が捗るArtistの話 - Qiita
python
1import matplotlib.animation as animation
2import matplotlib.pyplot as plt
3import numpy as np
4
5
6def f(x, y):
7 return np.sin(x) + np.cos(y)
8
9
10x = np.linspace(0, 2 * np.pi, 120)
11y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
12ims = []
13
14fig, [ax1, ax2] = plt.subplots(2, 1)
15
16for i in range(60):
17 x += np.pi / 15.
18 y += np.pi / 20.
19 img1 = ax1.imshow(f(x, y), animated=True)
20 img2 = ax2.imshow(f(x, y), animated=True)
21 ims.append([img1, img2])
22
23ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
24 repeat_delay=1000)
25ani.save('anim.gif', writer="imagemagick")
26# ani.save('anim.mp4', writer="ffmpeg")
27plt.show()
追記
カラーバーとタイトルをアニメーションに追加するのは少しややこしいです。
タイトルが全フレームで共通であれば、ループの外で set_title() を1度だけ呼べばよいですが、フレームごとにタイトルを変更するには以下の質問の回答のようにする必要があります。
Python - matplotlibのanimationで一枚ごとに違うタイトルを付けたい|teratail
また、カラーバーをループ内でつけると、どんどんカラーバーが増えてしまうという自体が起こるため、1度だけカラーバーをつけるようにする必要があります。
Python - matplotlibのfuncanimationでgifを作成すると、カラーバーが内にどんどんできていく。|teratail
python
1import matplotlib.animation as animation
2import matplotlib.pyplot as plt
3import numpy as np
4
5
6def f(x, y):
7 return np.sin(x) + np.cos(y)
8
9
10x = np.linspace(0, 2 * np.pi, 120)
11y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
12ims = []
13
14fig, [ax1, ax2] = plt.subplots(2, 1)
15fig.subplots_adjust(hspace=0.5)
16
17for i in range(60):
18 x += np.pi / 15.
19 y += np.pi / 20.
20 img1 = ax1.imshow(f(x, y), animated=True)
21 img2 = ax2.imshow(f(x, y), animated=True)
22
23 if i == 0: # 最初のループだけカラーバーをつける
24 fig.colorbar(img1, ax=ax1)
25 fig.colorbar(img2, ax=ax2)
26 # タイトル
27 title1 = ax1.text(0.5, 1.01, 'title1 i={:.2f}'.format(i),
28 ha='center', va='bottom',
29 transform=ax1.transAxes, fontsize='large')
30 title2 = ax2.text(0.5, 1.01, 'title2 i={:.2f}'.format(i),
31 ha='center', va='bottom',
32 transform=ax2.transAxes, fontsize='large')
33
34 ims.append([img1, img2, title1, title2])
35
36ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
37 repeat_delay=1000)
38ani.save('anim.gif', writer="imagemagick")
39# ani.save('anim.mp4', writer="ffmpeg")
40plt.show()
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/26 09:19
2019/04/26 09:43
2019/04/26 11:12
2019/04/26 16:19 編集
2019/04/27 04:53