voronoi
Voronoi Polygons for 2-D Point Sets
Author: Serge Rey (http://github.com/sjsrey)
import sys
import os
sys.path.append(os.path.abspath('..'))
import libpysal
from libpysal.cg.voronoi import voronoi, voronoi_frames
points = [(10.2, 5.1), (4.7, 2.2), (5.3, 5.7), (2.7, 5.3)]
regions, vertices = voronoi(points)
regions
vertices
region_df, point_df = voronoi_frames(points)
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
region_df.plot(ax=ax, color='blue',edgecolor='black', alpha=0.3)
point_df.plot(ax=ax, color='red')
n_points = 200
np.random.seed(12345)
points = np.random.random((n_points,2))*10 + 10
results = voronoi(points)
mins = points.min(axis=0)
maxs = points.max(axis=0)
regions, vertices = voronoi(points)
regions_df, points_df = voronoi_frames(points)
fig, ax = plt.subplots()
points_df.plot(ax=ax, color='red')
fig, ax = plt.subplots()
regions_df.plot(ax=ax, color='blue',edgecolor='black', alpha=0.3)
points_df.plot(ax=ax, color='red')
points = np.array(points)
maxs = points.max(axis=0)
mins = points.min(axis=0)
xr = maxs[0] - mins[0]
yr = maxs[1] - mins[1]
buff = 0.05
r = max(yr, xr) * buff
minx = mins[0] - r
miny = mins[1] - r
maxx = maxs[0] + r
maxy = maxs[1] + r
fig, ax = plt.subplots()
regions_df.plot(ax=ax, edgecolor='black', facecolor='blue', alpha=0.2 )
points_df.plot(ax=ax, color='red')
plt.xlim(minx, maxx)
plt.ylim(miny, maxy)
plt.title("buffer: %f, n: %d"%(r,n_points))
plt.show()
from libpysal.weights.contiguity import Voronoi as Vornoi_weights
w = Vornoi_weights(points)
w.n
w.pct_nonzero
w.histogram
idx = [i for i in range(w.n) if w.cardinalities[i]==12]
points[idx]