In [15]:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.style
matplotlib.style.available
%matplotlib inline
In [16]:
X, Y = np.mgrid[0:5, 0:5]
X, Y = np.meshgrid(sorted(np.random.random(5)), sorted(np.random.random(4)))
# X, Y = np.meshgrid()
In [17]:
def grid2enclosure(X, Y):
"""Generate cell boundaries """
# compute gradients in X and Y
Xx, Xy = np.gradient(X)
Yx, Yy = np.gradient(Y)
print(Xx.any(), Xy.any())
# lower left
ll = np.c_[(X - 0.5*Xx - 0.5*Xy).ravel(), (Y - 0.5*Yy - 0.5*Yx).ravel()]
# lower right
lr = np.c_[(X + 0.5*Xx - 0.5*Xy).ravel(), (Y - 0.5*Yy + 0.5*Yx).ravel()]
# upper left
ul = np.c_[(X - 0.5*Xx + 0.5*Xy).ravel(), (Y + 0.5*Yy - 0.5*Yx).ravel()]
# upper right
ur = np.c_[(X + 0.5*Xx + 0.5*Xy).ravel(), (Y + 0.5*Yy + 0.5*Yx).ravel()]
# number of cells x 5 points/cell x 2 (x,y)
verts = np.hstack(
[
ll[:,np.newaxis,:],
lr[:,np.newaxis,:],
ur[:,np.newaxis,:],
ul[:,np.newaxis,:],
ll[:,np.newaxis,:]
]
)
return verts
verts = grid2enclosure(X, Y)
In [18]:
plt.plot(X.ravel(), Y.ravel(), 'k.')
for vert in verts:
plt.fill(vert[:,0], vert[:,1], edgecolor='none', alpha=0.2)
In [19]:
import matplotlib.transforms
In [20]:
T = matplotlib.transforms.Affine2D()
T.rotate_around(1, 1, 0.1 * np.pi)
Out[20]:
In [21]:
XY_t = T.transform(np.c_[X.ravel(), Y.ravel()])
X_t, Y_t = XY_t[:,0].reshape(X.shape), XY_t[:,1].reshape(Y.shape)
vert_t = [T.transform(vert) for vert in verts]
vert_t = grid2enclosure(X_t, Y_t)
plt.plot(X_t, Y_t, 'k.')
for vert in vert_t:
plt.fill(vert[:,0], vert[:,1], edgecolor='none', alpha=0.2)
In [22]:
T.skew(0.8, 0.3)
Out[22]:
In [23]:
XY_t = T.transform(np.c_[X.ravel(), Y.ravel()])
X_t, Y_t = XY_t[:,0].reshape(X.shape), XY_t[:,1].reshape(Y.shape)
vert_t = [T.transform(vert) for vert in verts]
vert_t = grid2enclosure(X_t, Y_t)
plt.plot(X_t, Y_t, 'k.')
for vert in vert_t:
plt.fill(vert[:,0], vert[:,1], edgecolor='none', alpha=0.2)
In [24]:
# note that this has 1 cell less
mesh = plt.pcolormesh(X_t, Y_t, X_t)
In [25]:
import scipy.spatial
In [28]:
vor = scipy.spatial.Voronoi(np.c_[X_t.ravel(), Y_t.ravel()])
scipy.spatial.voronoi_plot_2d(vor)
Out[28]:
In [29]:
X = [1, 2, 4, 7]
Xx = np.gradient( [1, 2, 4, 7])
l = X - 0.5 * Xx
r = X + 0.5 * Xx
In [ ]:
In [ ]: