0
0
SciPydata~5 mins

Voronoi diagrams in SciPy

Choose your learning style9 modes available
Introduction

Voronoi diagrams help us divide space into areas based on distance to points. This is useful to find which point is closest to any location.

Finding the closest store to a customer's location on a map.
Dividing farmland into regions closest to each water source.
Planning cell tower coverage areas based on tower locations.
Assigning delivery zones to warehouses based on distance.
Syntax
SciPy
from scipy.spatial import Voronoi

vor = Voronoi(points)

# points is a 2D array of coordinates

You must provide points as a list or array of 2D coordinates.

The Voronoi object contains vertices and regions describing the diagram.

Examples
Create a Voronoi diagram from three points forming a triangle.
SciPy
from scipy.spatial import Voronoi
points = [[0, 0], [1, 0], [0, 1]]
vor = Voronoi(points)
Create a Voronoi diagram from 5 random points in 2D space.
SciPy
import numpy as np
points = np.random.rand(5, 2)
vor = Voronoi(points)
Sample Program

This program creates a Voronoi diagram from four points. It prints the vertices of the diagram, the regions defined by these vertices, and which region corresponds to each input point.

SciPy
import numpy as np
from scipy.spatial import Voronoi

# Define points representing locations
points = np.array([[1, 1], [3, 1], [2, 4], [5, 3]])

# Create Voronoi diagram
vor = Voronoi(points)

# Print vertices of the Voronoi diagram
print('Vertices:')
print(vor.vertices)

# Print regions (list of indices of vertices for each region)
print('\nRegions:')
print(vor.regions)

# Print point-region mapping
print('\nPoint to region mapping:')
print(vor.point_region)
OutputSuccess
Important Notes

Some regions may be empty or infinite, shown as empty lists or containing -1.

Vertices are points where edges of the Voronoi cells meet.

Use visualization tools like matplotlib to better understand the diagram.

Summary

Voronoi diagrams split space based on closest points.

Use scipy.spatial.Voronoi with 2D points to create diagrams.

Vertices and regions describe the shape and boundaries of cells.