teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

d

2018/11/20 08:39

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -22,4 +22,47 @@
22
22
  fig.show()
23
23
  ```
24
24
 
25
- ![イメージ説明](1dcde37fb0113624eb369c9581c02bc7.gif)
25
+ ![イメージ説明](1dcde37fb0113624eb369c9581c02bc7.gif)
26
+
27
+ ## 追記
28
+
29
+ Axes.set_title() の返り値 matplotlib.text.Text を Artists として入れてもタイトルはアニメーションできないようです。
30
+ 代わりに Axes.text() の返り値ならできるので、これでタイトルをアニメーションできました。
31
+
32
+ 参考
33
+ [matplotlib artist animation : title or text not changing](https://stackoverflow.com/questions/47421486/matplotlib-artist-animation-title-or-text-not-changing)
34
+
35
+ ```python
36
+ # Notebook で inline 表示する場合は以下が必要
37
+ %matplotlib nbagg
38
+
39
+ #import packages
40
+ import matplotlib.pyplot as plt
41
+ import numpy as np
42
+ from matplotlib.animation import ArtistAnimation
43
+
44
+ x = np.linspace(0, np.pi * 4, 100)
45
+
46
+ fig, ax = plt.subplots(figsize=(4, 4))
47
+ frames = [] # 各フレームを構成する Artist 一覧
48
+ for delta in np.linspace(0, np.pi, 30):
49
+ y = np.sin(x + delta)
50
+
51
+ # 折れ線グラフを作成する。
52
+ lines = ax.plot(x, y, c='b')
53
+ title = ax.text(0.5, 1.01, 'delta={:.2f}'.format(delta),
54
+ ha='center', va='bottom',
55
+ transform=ax.transAxes, fontsize='large')
56
+ frames.append(lines + [title])
57
+
58
+ # アニメーションを作成する。
59
+ anim = ArtistAnimation(fig, frames, interval=500)
60
+
61
+ # gif 画像として保存する。
62
+ anim.save('animation.gif', writer='imagemagick')
63
+
64
+ # Figure を表示する。
65
+ fig.show()
66
+ ```
67
+
68
+ ![イメージ説明](ce85ebd13baf84f2642435c64c2c95ed.gif)