Search code examples
pythonscipycomputational-geometryvoronoi

Filtering the voronoi diagram from python scipy


I am using the python scipy to compute the voronoi diagram from a set of points in 2-dimensions as follows:

import numpy as np
from scipy.spatial import Voronoi
x = np.random.uniform(-1, 1, (1000, 2))

v = Voronoi(x)

I got quite confused with the different attributes of the voronoi object. What I basically want to do now is filter out all the vertices which are beyond -0.5 and 0.5 in both the dimensions.


Solution

  • You'll have to explain what you mean by "filter" (filter out?). In any case you can obtain the vertices and several types of ridges of the voronoi shapes:

        verts = v.vertices
    

    , this will give an array with two columns (x and y coordinates for the vertices). You can mask them the same way you do with numpy arrays (like verts[:,0] > -0.5) and use them for whatever you wish.

    I'm not entirely sure if this answers your question but you can find a pretty good tutorial here, including plotting.