In [1]:
from scipy.spatial import Delaunay, delaunay_plot_2d, Voronoi, voronoi_plot_2d
import matplotlib.pyplot as plt
import numpy as np
In [2]:
w = h = 360
In [3]:
n = 6
np.random.seed(0)
pts = np.random.randint(0, w, (n, 2))
In [4]:
print(pts)
In [5]:
print(type(pts))
In [6]:
print(pts.shape)
In [7]:
tri = Delaunay(pts)
In [8]:
print(type(tri))
In [9]:
fig = delaunay_plot_2d(tri)
fig.savefig('data/dst/scipy_matplotlib_delaunay.png')
plt.close()
In [10]:
print(tri.points)
In [11]:
print(tri.points == pts)
In [12]:
print(tri.simplices)
In [13]:
print(pts[tri.simplices])
In [14]:
vor = Voronoi(pts)
In [15]:
print(type(vor))
In [16]:
fig = voronoi_plot_2d(vor)
fig.savefig('data/dst/scipy_matplotlib_voronoi.png')
plt.close()
In [17]:
print(vor.vertices)
In [18]:
print(vor.regions)
In [19]:
print([r for r in vor.regions if -1 not in r and r])
In [20]:
for region in [r for r in vor.regions if -1 not in r and r]:
print(vor.vertices[region])
In [21]:
fig, ax = plt.subplots()
voronoi_plot_2d(vor, ax)
for region, c in zip([r for r in vor.regions if -1 not in r and r], ['yellow', 'pink']):
ax.fill(vor.vertices[region][:, 0],
vor.vertices[region][:, 1],
color=c)
fig.savefig('data/dst/scipy_matplotlib_voronoi_fill.png')
plt.close()
In [22]:
fig, ax = plt.subplots(figsize=(4, 4))
delaunay_plot_2d(tri, ax)
voronoi_plot_2d(vor, ax, show_vertices=False)
ax.set_xlim(0, w)
ax.set_ylim(0, h)
ax.grid(linestyle='--')
fig.savefig('data/dst/scipy_matplotlib_delaunay_voronoi.png')
plt.close()