Gabriel - 7 months ago 151

Python Question

I'm generating a simple 2D Voronoi tessellation, using the scipy.spatial.Voronoi function. I use a random 2D distribution of points (see MWE below).

I need a way to go through each defined region (defined by

`scipy.spatial.Voronoi`

The issue is that there are

`N+1`

`N`

Here's a MWE that will fail when it gets to the last region:

`from scipy.spatial import Voronoi`

import numpy as np

# Generate random data.

N = 10

x = [np.random.random() for i in xrange(N)]

y = [np.random.random() for i in xrange(N)]

points = zip(x, y)

# Obtain Voronoi regions.

vor = Voronoi(points)

# Loop through each defined region/polygon

for i, reg in enumerate(vor.regions):

print 'Region:', i

print 'Indices of vertices of Voronoi region:', reg

print 'Associated point:', points[i], '\n'

Another thing I don't understand is why are there empty

`vor.regions`

regions: Indices of the Voronoi vertices forming each Voronoi region. -1 indicates vertex outside the Voronoi diagram.

What does an empty region mean?

I tried the

`point_region`

`points`

`10`

Answer

My bad, I was misreading the docs. It says:

point_region:

Index of the.Voronoi regionfor eachinput point

and I was using `point_region`

it as if it were the: "*Index of the input point for each Voronoi region*".

Instead of using:

```
points[i]
```

the correct point coordinates for each region can be obtained with:

```
np.where(vor.point_region == i)[0][0]
```