I use Voronoi from scipy.spatial to draw points and polygons. Now I need export table/dataframe which link coordinates of initial points to polygon vertices coordinates but I cannot find corresponding formula from attributes (points, vertices, regions etc), can somebody share it ?
Asked
Active
Viewed 812 times
1 Answers
1
From the results of scipy.spatial.Voronoi
, the association between the input point and the regions of the Voronoi diagram are provided in the point_region
attribute. Specifically, given the index of an input point, the point_region
attribute gives the index of the region associated with that point, the regions
attribute can be used to find indices of the Voronoi vertices associated with the region and then vertices
attribute can then be used to get the coordinates of the various Voronoi vertices.
Here is an example:
import numpy as np
points = np.array([[0, .2], [-.2, 1], [0, 2.1], [1.1, -.1], [1, 1], [0.9, 1.9],[2.2, -0.2], [1.9, 1.1], [2.1, 2.3]])
from scipy.spatial import Voronoi, voronoi_plot_2d
vor = Voronoi(points)
print("points[4] =", points[4])
print("region for point 4 =", vor.point_region[4])
print("vertices for region for point 4 =", vor.regions[vor.point_region[4]])
print("coordinate of Voronoi vertices associated with point 4:")
print(vor.vertices[vor.regions[vor.point_region[4]]])
import matplotlib.pyplot as plt
fig = voronoi_plot_2d(vor)
ax = plt.gca()
ax.set_aspect('equal', adjustable='box')
plt.show()
The output from this code is:
points[4] = [1. 1.]
region for point 4 = 8
vertices for region for point 4 = [9, 4, 6, 0, 7]
coordinate of Voronoi vertices associated with point 4:
[[1.512 0.492 ]
[1.4 1.5 ]
[0.4 1.38888889]
[0.4 0.725 ]
[0.64915254 0.41355932]]
The Voronoi diagram in the example is shown below: the example is querying information about the Voronoi region in the center.

Alex
- 1,042
- 1
- 8
- 17