回答編集履歴

1

d

2018/11/20 08:39

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -47,3 +47,89 @@
47
47
 
48
48
 
49
49
  ![イメージ説明](1dcde37fb0113624eb369c9581c02bc7.gif)
50
+
51
+
52
+
53
+ ## 追記
54
+
55
+
56
+
57
+ Axes.set_title() の返り値 matplotlib.text.Text を Artists として入れてもタイトルはアニメーションできないようです。
58
+
59
+ 代わりに Axes.text() の返り値ならできるので、これでタイトルをアニメーションできました。
60
+
61
+
62
+
63
+ 参考
64
+
65
+ [matplotlib artist animation : title or text not changing](https://stackoverflow.com/questions/47421486/matplotlib-artist-animation-title-or-text-not-changing)
66
+
67
+
68
+
69
+ ```python
70
+
71
+ # Notebook で inline 表示する場合は以下が必要
72
+
73
+ %matplotlib nbagg
74
+
75
+
76
+
77
+ #import packages
78
+
79
+ import matplotlib.pyplot as plt
80
+
81
+ import numpy as np
82
+
83
+ from matplotlib.animation import ArtistAnimation
84
+
85
+
86
+
87
+ x = np.linspace(0, np.pi * 4, 100)
88
+
89
+
90
+
91
+ fig, ax = plt.subplots(figsize=(4, 4))
92
+
93
+ frames = [] # 各フレームを構成する Artist 一覧
94
+
95
+ for delta in np.linspace(0, np.pi, 30):
96
+
97
+ y = np.sin(x + delta)
98
+
99
+
100
+
101
+ # 折れ線グラフを作成する。
102
+
103
+ lines = ax.plot(x, y, c='b')
104
+
105
+ title = ax.text(0.5, 1.01, 'delta={:.2f}'.format(delta),
106
+
107
+ ha='center', va='bottom',
108
+
109
+ transform=ax.transAxes, fontsize='large')
110
+
111
+ frames.append(lines + [title])
112
+
113
+
114
+
115
+ # アニメーションを作成する。
116
+
117
+ anim = ArtistAnimation(fig, frames, interval=500)
118
+
119
+
120
+
121
+ # gif 画像として保存する。
122
+
123
+ anim.save('animation.gif', writer='imagemagick')
124
+
125
+
126
+
127
+ # Figure を表示する。
128
+
129
+ fig.show()
130
+
131
+ ```
132
+
133
+
134
+
135
+ ![イメージ説明](ce85ebd13baf84f2642435c64c2c95ed.gif)