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

回答編集履歴

4

d

2020/05/18 14:01

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -1,29 +1,61 @@
1
- FuncAnimation を使えば、アニメーション化できると思います。
1
+ [FuncAnimation](https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.animation.FuncAnimation.html) を使います。
2
2
 
3
3
 
4
+ 1. `frame` 引数には、ジェネレーターが指定できるので、各イテレーションごとの Voronoi オブジェクトを返すジェネレーターを指定する。
5
+ 2. `func` 引数に、受け取った Voronoi オブジェクトを描画する関数を指定する。
6
+
7
+ コードにすると以下のようになります。
8
+
4
9
  ```python
10
+ import random
5
11
  import matplotlib.pyplot as plt
6
- import numpy as np
7
- from matplotlib.animation import FuncAnimation
8
12
  from scipy.spatial import Voronoi, voronoi_plot_2d
13
+ from shapely.geometry import Polygon, Point
9
14
 
10
- fig, ax = plt.subplots(figsize=(9, 7))
11
15
 
16
+ def centroidal(vor, pts):
17
+ sq = Polygon([[0, 0], [1, 0], [1, 1], [0, 1]])
12
- n = 100
18
+ maxd = 0.0
19
+ for i in range(len(pts) - 3):
20
+ poly = [vor.vertices[v] for v in vor.regions[vor.point_region[i]]]
21
+ i_cell = sq.intersection(Polygon(poly))
22
+ p = Point(pts[i])
23
+ pts[i] = i_cell.centroid.coords[0]
24
+ d = p.distance(Point(pts[i]))
25
+ if maxd < d:
26
+ maxd = d
27
+ return maxd
13
28
 
14
29
 
15
- def plot(frame):
30
+ def process():
16
- ax.cla() # Axes をクリアする
17
- points = np.random.rand(n, 2)
18
- vor = Voronoi(points)
19
- voronoi_plot_2d(vor, ax, show_vertices=False)
20
- ax.set_xlim(0, 1)
31
+ d_threshold = 0.001
21
- ax.set_ylim(0, 1)
22
32
 
33
+ for i in range(100):
34
+ vor = Voronoi(pts)
35
+ d = centroidal(vor, pts)
36
+ if d < d_threshold:
37
+ break
23
38
 
39
+ yield vor
40
+
41
+
42
+ def plot(vor):
43
+ ax.cla()
44
+ voronoi_plot_2d(vor, ax=ax, show_vertices=False)
45
+ ax.set_aspect("equal")
46
+ ax.set_xlim([0, 1])
47
+ ax.set_ylim([0, 1])
48
+
49
+
50
+ n = 30
51
+ pts = [[random.random(), random.random()] for i in range(n)]
52
+ pts = pts + [[100, 100], [100, -100], [-100, 0]]
53
+
54
+ fig, ax = plt.subplots(figsize=(9, 9))
55
+
24
56
  # アニメーションを作成する。
25
- anim = FuncAnimation(fig, plot, frames=20)
57
+ anim = FuncAnimation(fig, plot, frames=process, save_count=n)
26
- anim.save("animation.gif", writer="pillow", fps=5)
58
+ anim.save("animation.gif", writer="pillow")
27
59
  ```
28
60
 
29
- ![イメージ説明](a80a4015d74bb4877e3096602c1e324c.gif)
61
+ ![イメージ説明](0a841c3a68bd8d0c9d67165cd7d51150.gif)

3

d

2020/05/18 14:01

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -24,4 +24,6 @@
24
24
  # アニメーションを作成する。
25
25
  anim = FuncAnimation(fig, plot, frames=20)
26
26
  anim.save("animation.gif", writer="pillow", fps=5)
27
- ```
27
+ ```
28
+
29
+ ![イメージ説明](a80a4015d74bb4877e3096602c1e324c.gif)

2

d

2020/05/18 13:21

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -1,3 +1,27 @@
1
- ----
1
+ FuncAnimation を使えば、アニメーション化できると思います。
2
2
 
3
+
4
+ ```python
5
+ import matplotlib.pyplot as plt
6
+ import numpy as np
7
+ from matplotlib.animation import FuncAnimation
8
+ from scipy.spatial import Voronoi, voronoi_plot_2d
9
+
3
- すいません。別の質問の回答と間違えて回答してしまったので書き直します。
10
+ fig, ax = plt.subplots(figsize=(9, 7))
11
+
12
+ n = 100
13
+
14
+
15
+ def plot(frame):
16
+ ax.cla() # Axes をクリアする
17
+ points = np.random.rand(n, 2)
18
+ vor = Voronoi(points)
19
+ voronoi_plot_2d(vor, ax, show_vertices=False)
20
+ ax.set_xlim(0, 1)
21
+ ax.set_ylim(0, 1)
22
+
23
+
24
+ # アニメーションを作成する。
25
+ anim = FuncAnimation(fig, plot, frames=20)
26
+ anim.save("animation.gif", writer="pillow", fps=5)
27
+ ```

1

d

2020/05/18 13:21

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -1,35 +1,3 @@
1
- 以下のような感じでどうでしょうか。
1
+ ----
2
- vmin, vmax を指定すると、カラーバーの範囲が [vmin, vmax] の範囲になるはずです。
3
2
 
4
- ```python
5
- import matplotlib.pyplot as plt
6
- import numpy as np
7
- from matplotlib.animation import FuncAnimation
8
-
9
- fig, ax = plt.subplots(figsize=(9, 7))
3
+ すいません。別の質問の回答と間違えて回答してしまったので書き直します。
10
-
11
- cbar_initialized = False # カラーバーを初期化したかどうか
12
-
13
-
14
- def plot(frame):
15
- global cbar_initialized
16
-
17
- ax.cla() # Axes をクリアする
18
-
19
- # pcolormesh を作成する。
20
- X, Y = np.mgrid[-10:11, -10:11]
21
- Z = np.random.randint(-15, 15, X.shape)
22
- mesh = ax.pcolormesh(X, Y, Z, cmap="jet", vmin=0, vmax=5)
23
-
24
- if not cbar_initialized:
25
- # カラーバーは1回だけ初期化する。
26
- cbar_initialized = True
27
- fig.colorbar(mesh, ax=ax)
28
-
29
-
30
- # アニメーションを作成する。
31
- anim = FuncAnimation(fig, plot, frames=20)
32
- anim.save("animation.gif", writer="pillow", fps=5)
33
- ```
34
-
35
- ![イメージ説明](84bc6c77ee4a64a287c456d91b1d9367.gif)