回答編集履歴
2
d
answer
CHANGED
@@ -42,7 +42,7 @@
|
|
42
42
|
|
43
43
|
カラーバーとタイトルをアニメーションに追加するのは少しややこしいです。
|
44
44
|
|
45
|
-
タイトルが全フレームで共通であれば、ループの外で set_title() を1度だけ呼べばよいですが、フレームごとに
|
45
|
+
タイトルが全フレームで共通であれば、ループの外で set_title() を1度だけ呼べばよいですが、フレームごとにタイトルを変更するには以下の質問の回答のようにする必要があります。
|
46
46
|
|
47
47
|
[Python - matplotlibのanimationで一枚ごとに違うタイトルを付けたい|teratail](https://teratail.com/questions/159258)
|
48
48
|
|
@@ -73,7 +73,7 @@
|
|
73
73
|
img1 = ax1.imshow(f(x, y), animated=True)
|
74
74
|
img2 = ax2.imshow(f(x, y), animated=True)
|
75
75
|
|
76
|
-
if i == 0:
|
76
|
+
if i == 0: # 最初のループだけカラーバーをつける
|
77
77
|
fig.colorbar(img1, ax=ax1)
|
78
78
|
fig.colorbar(img2, ax=ax2)
|
79
79
|
# タイトル
|
1
d
answer
CHANGED
@@ -36,4 +36,61 @@
|
|
36
36
|
plt.show()
|
37
37
|
```
|
38
38
|
|
39
|
-

|
39
|
+

|
40
|
+
|
41
|
+
## 追記
|
42
|
+
|
43
|
+
カラーバーとタイトルをアニメーションに追加するのは少しややこしいです。
|
44
|
+
|
45
|
+
タイトルが全フレームで共通であれば、ループの外で set_title() を1度だけ呼べばよいですが、フレームごとに返るには以下の質問の回答のようにする必要があります。
|
46
|
+
|
47
|
+
[Python - matplotlibのanimationで一枚ごとに違うタイトルを付けたい|teratail](https://teratail.com/questions/159258)
|
48
|
+
|
49
|
+
また、カラーバーをループ内でつけると、どんどんカラーバーが増えてしまうという自体が起こるため、1度だけカラーバーをつけるようにする必要があります。
|
50
|
+
|
51
|
+
[Python - matplotlibのfuncanimationでgifを作成すると、カラーバーが内にどんどんできていく。|teratail](https://teratail.com/questions/148292)
|
52
|
+
|
53
|
+
```python
|
54
|
+
import matplotlib.animation as animation
|
55
|
+
import matplotlib.pyplot as plt
|
56
|
+
import numpy as np
|
57
|
+
|
58
|
+
|
59
|
+
def f(x, y):
|
60
|
+
return np.sin(x) + np.cos(y)
|
61
|
+
|
62
|
+
|
63
|
+
x = np.linspace(0, 2 * np.pi, 120)
|
64
|
+
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
|
65
|
+
ims = []
|
66
|
+
|
67
|
+
fig, [ax1, ax2] = plt.subplots(2, 1)
|
68
|
+
fig.subplots_adjust(hspace=0.5)
|
69
|
+
|
70
|
+
for i in range(60):
|
71
|
+
x += np.pi / 15.
|
72
|
+
y += np.pi / 20.
|
73
|
+
img1 = ax1.imshow(f(x, y), animated=True)
|
74
|
+
img2 = ax2.imshow(f(x, y), animated=True)
|
75
|
+
|
76
|
+
if i == 0:
|
77
|
+
fig.colorbar(img1, ax=ax1)
|
78
|
+
fig.colorbar(img2, ax=ax2)
|
79
|
+
# タイトル
|
80
|
+
title1 = ax1.text(0.5, 1.01, 'title1 i={:.2f}'.format(i),
|
81
|
+
ha='center', va='bottom',
|
82
|
+
transform=ax1.transAxes, fontsize='large')
|
83
|
+
title2 = ax2.text(0.5, 1.01, 'title2 i={:.2f}'.format(i),
|
84
|
+
ha='center', va='bottom',
|
85
|
+
transform=ax2.transAxes, fontsize='large')
|
86
|
+
|
87
|
+
ims.append([img1, img2, title1, title2])
|
88
|
+
|
89
|
+
ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
|
90
|
+
repeat_delay=1000)
|
91
|
+
ani.save('anim.gif', writer="imagemagick")
|
92
|
+
# ani.save('anim.mp4', writer="ffmpeg")
|
93
|
+
plt.show()
|
94
|
+
```
|
95
|
+
|
96
|
+

|