0
0
SciPydata~10 mins

Voronoi diagrams in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Voronoi diagrams
Input points
Compute Voronoi
Find edges and vertices
Plot Voronoi cells
Visualize regions
Start with points, compute Voronoi regions dividing space, then plot edges and vertices to visualize.
Execution Sample
SciPy
from scipy.spatial import Voronoi, voronoi_plot_2d
import matplotlib.pyplot as plt
points = [[1,1], [2,3], [3,1], [5,4]]
vor = Voronoi(points)
voronoi_plot_2d(vor)
plt.show()
This code creates Voronoi regions from 4 points and plots the diagram.
Execution Table
StepActionInputOutputNotes
1Input points[[1,1],[2,3],[3,1],[5,4]]Points arrayPoints to build Voronoi from
2Compute VoronoiPoints arrayVoronoi objectCalculates edges, vertices, regions
3Extract verticesVoronoi objectVertices arrayCoordinates of Voronoi vertices
4Extract ridge verticesVoronoi objectPairs of vertex indicesEdges between vertices
5Plot VoronoiVoronoi objectPlot on matplotlibVisualizes cells and edges
6Show plotPlotWindow with diagramDisplays Voronoi diagram
7EndN/AN/AExecution complete
💡 All points processed and Voronoi diagram displayed
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
points[[1,1],[2,3],[3,1],[5,4]][[1,1],[2,3],[3,1],[5,4]][[1,1],[2,3],[3,1],[5,4]][[1,1],[2,3],[3,1],[5,4]][[1,1],[2,3],[3,1],[5,4]]
vorN/AVoronoi objectVoronoi objectVoronoi objectVoronoi object
verticesN/AN/A[array of vertex coords][array of vertex coords][array of vertex coords]
ridge_verticesN/AN/AN/A[list of vertex index pairs][list of vertex index pairs]
Key Moments - 2 Insights
Why do some Voronoi vertices have coordinates outside the range of input points?
Voronoi vertices can lie outside the convex hull of input points because the diagram extends infinitely; see execution_table step 3 where vertices include points beyond inputs.
What does a ridge vertex pair of [-1, 2] mean in the Voronoi object?
A -1 index means the edge extends to infinity in that direction; this is shown in execution_table step 4 where ridge vertices include -1 for infinite edges.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is stored in 'vertices'?
ACoordinates of Voronoi vertices
BInput points
CPairs of ridge vertices
DPlot object
💡 Hint
Check the 'Output' column in step 3 of execution_table
At which step does the code calculate edges between Voronoi vertices?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Extract ridge vertices' in execution_table step 4
If we add more points, how will the 'vertices' variable change after step 3?
AIt will have fewer vertices
BIt will have more vertices
CIt will stay the same
DIt will become empty
💡 Hint
More points create more Voronoi regions and vertices, see variable_tracker for 'vertices'
Concept Snapshot
Voronoi diagrams split space by closest points.
Use scipy.spatial.Voronoi(points) to compute.
Vertices are intersection points of edges.
Edges can be finite or infinite (-1 index).
Plot with voronoi_plot_2d(vor) and plt.show().
Full Transcript
Voronoi diagrams divide space into regions closest to each input point. We start by giving a list of points. Then, scipy's Voronoi function calculates the edges and vertices that form these regions. Some vertices can be outside the input points range because the diagram extends infinitely. Edges are pairs of vertices; if an edge extends infinitely, one vertex index is -1. Finally, we plot the diagram using voronoi_plot_2d and show it with matplotlib. This process helps visualize how space is split by proximity to points.