Planar Voronoi Diagrams

Plotting the generator points


In [1]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline

points = np.array([[1.5, 1.], [3.5, 1.], [5., 2.], [2.5, 3.], [3.5, 1.], 
                   [4., 4.], [5.5, 3.5], [6., 5.5], [7.5, 4], [1.6, 6.]])

fig = plt.figure()
ax = fig.add_subplot('111')
ax.plot(points[:, 0], points[:, 1], 'o', color='k')
ax.set_xlim([-1, 9])
ax.set_ylim([-1, 9])


Out[1]:
(-1, 9)

Plotting the Voronoi Diagram


In [2]:
from scipy.spatial import Voronoi, voronoi_plot_2d
vor = Voronoi(points)

fig = plt.figure()
ax = fig.add_subplot('111')
ax.plot(points[:, 0], points[:, 1], 'o', color='k')
ax.set_xlim([-1, 9])
ax.set_ylim([-1, 9])
voronoi_plot_2d(vor, ax)


Out[2]:

Plotting a Delaunay triangulation


In [3]:
from scipy.spatial import Delaunay
tri = Delaunay(points)

fig = plt.figure()
ax = fig.add_subplot('111')
ax.plot(points[:, 0], points[:, 1], 'o', color='k')
ax.set_xlim([-1, 9])
ax.set_ylim([-1, 9])
ax.triplot(points[:,0], points[:,1], tri.simplices.copy(), color='blue')


Out[3]:
[<matplotlib.lines.Line2D at 0x7e58ba8>,
 <matplotlib.lines.Line2D at 0x7e58e10>]

Plotting both graphs


In [4]:
fig = plt.figure()
ax = fig.add_subplot('111')
ax.plot(points[:, 0], points[:, 1], 'o', color='k')
ax.set_xlim([-1, 9])
ax.set_ylim([-1, 9])
voronoi_plot_2d(vor, ax)
ax.triplot(points[:,0], points[:,1], tri.simplices.copy(), color='blue')


Out[4]:
[<matplotlib.lines.Line2D at 0x7e225c0>,
 <matplotlib.lines.Line2D at 0x77d24a8>]

In [ ]: