In [1]:
import shapely
import shapely.geometry

In [2]:
# for plotting polygons
import descartes

In [3]:
# Start with 2 points
point1 = shapely.geometry.Point(1,1)
point2 = shapely.geometry.Point(2,1)

In [4]:
# plot them in matplotlib
fig, ax = plt.subplots(1,1, figsize=(6,6))
ax.set_xlim(0,3)
ax.set_ylim(0,3)

ax.plot(point1.x, point1.y, 'ro')
ax.plot(point2.x, point2.y, 'bo')


Out[4]:
[<matplotlib.lines.Line2D at 0x104d69110>]

In [5]:
# add a buffer
poly1 = point1.buffer(1)
# and a box around it
poly2 = point2.buffer(1).envelope
# plot them using descartes
ax.add_patch(descartes.PolygonPatch(poly1, alpha=0.3, color='red'))
ax.add_patch(descartes.PolygonPatch(poly2, alpha=0.3, color='blue'))
fig


Out[5]:

In [6]:
# compute the intersection and add it on top
poly1_and_poly2 = poly1.intersection(poly2)
patch = descartes.PolygonPatch(poly1_and_poly2, alpha=0.5, facecolor='white', edgecolor='green', linewidth=3)
ax.add_patch(patch)
fig


Out[6]:

In [7]:
# let's warp a bit
from shapely.affinity import skew, translate, scale 
poly3 = skew(scale(translate(poly1, xoff=0.5, yoff=1), xfact=0.5, yfact=0.8), ys=-0.125*np.pi, origin=(1,1), use_radians=True)
patch = descartes.PolygonPatch(poly3, alpha=0.5, color='yellow')
ax.add_patch(patch)
fig


Out[7]:

In [8]:
# add all the polygons together
import shapely.ops
total = shapely.ops.cascaded_union([poly1, poly2, poly3])
total = total.buffer(-0.25)
patch = descartes.PolygonPatch(total, facecolor="black", alpha=0.5)
ax.add_patch(patch)
fig


Out[8]: