質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

3768閲覧

matplotlib.animationでfigureオブジェクトのアニメーションを作りたい

sodiumplus3

総合スコア71

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/04/26 07:52

編集2019/04/26 10:41

###matplotlib.animation.ArtistAnimationについて

https://qiita.com/msrks/items/e264872efa062c7d6955

上記のサイトの例2を参考にして、アニメーションを作成したいと思っています。
imに参考サイトではplt.imshow()で作られるオブジェクトが入っていますが、plt.figure()で作られるオブジェクトを入れたく、以下のコードを書きましたが、

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 = [] for i in range(60): x += np.pi / 15. y += np.pi / 20. # *********** fig2 = plt.figure() ax1 = fig2.add_subplot(2,1,1) ax2 = fig2.add_subplot(2,1,2) im1 = ax1.imshow(f(x, y), animated=True) im2 = ax1.imshow(f(x, y), animated=True) ims.append([fig2]) #************* ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True, repeat_delay=1000) ani.save('anim.gif', writer="imagemagick") # ani.save('anim.mp4', writer="ffmpeg") plt.show()

自分で変えた部分は***で囲った部分です。
実行するとIndexError: list index out of rangeが出てしまうのですが、どうすれば良いかわかりますか?

###追記
タイトルを教えていただいた通りのコードで実行すると画像のようになってしまうのですが、解決方法はありますか?
/Users/keisuke/Desktop/ 2019-04-26 19.39.04.png

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

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 08:47

編集2019/04/26 09:43
tiitoi

総合スコア21956

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sodiumplus3

2019/04/26 09:19

ありがとうございます!無事理解できました。追加で一つだけ質問させてください。カラーバーやタイトルなどをそれぞれにつける方法はありますか?(長くなるようでしたら改めて質問をたてさせていただきますのでそう言っていただいて大丈夫です)
tiitoi

2019/04/26 09:43

回答に追記しました。
sodiumplus3

2019/04/26 11:12

ありがとうございます。追記したのですが、こちらの環境ではタイトルプロットが残って積み重なっていってしまうのですが、何か解決策はありますか?
tiitoi

2019/04/26 16:19 編集

Linux (matplotlib 3.0.3) 及び Windows (matplotlib 3.0.3) の Jupyter Notebook 上で回答追記のコードを実行し、生成された anim.gif をビューアーで確認しましたが、質問のような現象は再現できません。 すみませんが、OS や matplotlib のバージョンはいくつでしょうか? また確認した手順としては、追記のコードをコピペして実行し、生成された anim.gif を確認したということであっていますでしょうか
sodiumplus3

2019/04/27 04:53

MacOS mojave10.14.3とmatplotlib2.2.2です。anim.gifの方はうまくいっていましたので解決しました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問