回答編集履歴
2
d
test
CHANGED
@@ -86,7 +86,7 @@
|
|
86
86
|
|
87
87
|
|
88
88
|
|
89
|
-
タイトルが全フレームで共通であれば、ループの外で set_title() を1度だけ呼べばよいですが、フレームごとに
|
89
|
+
タイトルが全フレームで共通であれば、ループの外で set_title() を1度だけ呼べばよいですが、フレームごとにタイトルを変更するには以下の質問の回答のようにする必要があります。
|
90
90
|
|
91
91
|
|
92
92
|
|
@@ -148,7 +148,7 @@
|
|
148
148
|
|
149
149
|
|
150
150
|
|
151
|
-
if i == 0:
|
151
|
+
if i == 0: # 最初のループだけカラーバーをつける
|
152
152
|
|
153
153
|
fig.colorbar(img1, ax=ax1)
|
154
154
|
|
1
d
test
CHANGED
@@ -75,3 +75,117 @@
|
|
75
75
|
|
76
76
|
|
77
77
|
![イメージ説明](58b24ba4cd57a77a0e084c327dd56db3.gif)
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
## 追記
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
カラーバーとタイトルをアニメーションに追加するのは少しややこしいです。
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
タイトルが全フレームで共通であれば、ループの外で set_title() を1度だけ呼べばよいですが、フレームごとに返るには以下の質問の回答のようにする必要があります。
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
[Python - matplotlibのanimationで一枚ごとに違うタイトルを付けたい|teratail](https://teratail.com/questions/159258)
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
また、カラーバーをループ内でつけると、どんどんカラーバーが増えてしまうという自体が起こるため、1度だけカラーバーをつけるようにする必要があります。
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
[Python - matplotlibのfuncanimationでgifを作成すると、カラーバーが内にどんどんできていく。|teratail](https://teratail.com/questions/148292)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
```python
|
106
|
+
|
107
|
+
import matplotlib.animation as animation
|
108
|
+
|
109
|
+
import matplotlib.pyplot as plt
|
110
|
+
|
111
|
+
import numpy as np
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
def f(x, y):
|
118
|
+
|
119
|
+
return np.sin(x) + np.cos(y)
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
x = np.linspace(0, 2 * np.pi, 120)
|
126
|
+
|
127
|
+
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
|
128
|
+
|
129
|
+
ims = []
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
fig, [ax1, ax2] = plt.subplots(2, 1)
|
134
|
+
|
135
|
+
fig.subplots_adjust(hspace=0.5)
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
for i in range(60):
|
140
|
+
|
141
|
+
x += np.pi / 15.
|
142
|
+
|
143
|
+
y += np.pi / 20.
|
144
|
+
|
145
|
+
img1 = ax1.imshow(f(x, y), animated=True)
|
146
|
+
|
147
|
+
img2 = ax2.imshow(f(x, y), animated=True)
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
if i == 0:
|
152
|
+
|
153
|
+
fig.colorbar(img1, ax=ax1)
|
154
|
+
|
155
|
+
fig.colorbar(img2, ax=ax2)
|
156
|
+
|
157
|
+
# タイトル
|
158
|
+
|
159
|
+
title1 = ax1.text(0.5, 1.01, 'title1 i={:.2f}'.format(i),
|
160
|
+
|
161
|
+
ha='center', va='bottom',
|
162
|
+
|
163
|
+
transform=ax1.transAxes, fontsize='large')
|
164
|
+
|
165
|
+
title2 = ax2.text(0.5, 1.01, 'title2 i={:.2f}'.format(i),
|
166
|
+
|
167
|
+
ha='center', va='bottom',
|
168
|
+
|
169
|
+
transform=ax2.transAxes, fontsize='large')
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
ims.append([img1, img2, title1, title2])
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
|
178
|
+
|
179
|
+
repeat_delay=1000)
|
180
|
+
|
181
|
+
ani.save('anim.gif', writer="imagemagick")
|
182
|
+
|
183
|
+
# ani.save('anim.mp4', writer="ffmpeg")
|
184
|
+
|
185
|
+
plt.show()
|
186
|
+
|
187
|
+
```
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
![イメージ説明](72f0faa2ed5b7f3aa0a5134ee64af537.gif)
|