9951 explained code solutions for 126 technologies


python-scipyHow can I use Python and SciPy to generate a Voronoi diagram?


To generate a Voronoi diagram with Python and SciPy, you can use the scipy.spatial.Voronoi class. This class takes a list of coordinates as input and returns a collection of vertices and regions.

Below is an example of code that generates a Voronoi diagram with 10 randomly generated points:

import numpy as np
from scipy.spatial import Voronoi

# Generate 10 random points
points = np.random.rand(10,2)

# Create a Voronoi object
vor = Voronoi(points)

# Plot the diagram
import matplotlib.pyplot as plt

plt.plot(points[:,0], points[:,1], 'b.')
plt.plot(vor.vertices[:,0], vor.vertices[:,1], 'g-')

for simplex in vor.ridge_vertices:
    simplex = np.asarray(simplex)
    plt.plot(vor.vertices[simplex,0], vor.vertices[simplex,1], 'k-')

plt.show()

This code will generate an output like this:

Voronoi Diagram

The code is composed of the following parts:

  1. import numpy as np and from scipy.spatial import Voronoi: imports the necessary modules for generating the Voronoi diagram.
  2. points = np.random.rand(10,2): generates 10 random points.
  3. vor = Voronoi(points): creates a Voronoi object using the Voronoi class.
  4. plt.plot(points[:,0], points[:,1], 'b.'): plots the points.
  5. plt.plot(vor.vertices[:,0], vor.vertices[:,1], 'g-'): plots the vertices of the Voronoi diagram.
  6. for simplex in vor.ridge_vertices:: iterates over the ridge vertices of the Voronoi diagram.
  7. plt.plot(vor.vertices[simplex,0], vor.vertices[simplex,1], 'k-'): plots the ridge vertices.
  8. plt.show(): displays the plot.

Helpful links

Edit this code on GitHub