Author: Serge Rey (http://github.com/sjsrey)
In [24]:
import sys
import os
sys.path.append(os.path.abspath('..'))
import libpysal
In [25]:
from libpysal.cg.voronoi import voronoi, voronoi_frames
In [26]:
points = [(10.2, 5.1), (4.7, 2.2), (5.3, 5.7), (2.7, 5.3)]
In [27]:
regions, vertices = voronoi(points)
In [28]:
regions
Out[28]:
In [29]:
vertices
Out[29]:
In [30]:
region_df, point_df = voronoi_frames(points)
In [31]:
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
In [32]:
fig, ax = plt.subplots()
region_df.plot(ax=ax, color='blue',edgecolor='black', alpha=0.3)
point_df.plot(ax=ax, color='red')
Out[32]:
In [33]:
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)
In [34]:
regions, vertices = voronoi(points)
In [35]:
regions_df, points_df = voronoi_frames(points)
In [36]:
fig, ax = plt.subplots()
points_df.plot(ax=ax, color='red')
Out[36]:
In [37]:
fig, ax = plt.subplots()
regions_df.plot(ax=ax, color='blue',edgecolor='black', alpha=0.3)
points_df.plot(ax=ax, color='red')
Out[37]:
In [38]:
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
In [39]:
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()
In [40]:
from libpysal.weights.contiguity import Voronoi as Vornoi_weights
In [41]:
w = Vornoi_weights(points)
In [42]:
w.n
Out[42]:
In [43]:
w.pct_nonzero
Out[43]:
In [44]:
w.histogram
Out[44]:
In [45]:
idx = [i for i in range(w.n) if w.cardinalities[i]==12]
In [46]:
points[idx]
Out[46]:
In [ ]: