import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
# Create triangulation of random (x,y) points.
np.random.seed(0)
x = np.random.rand(10)
y = np.random.rand(10)
triang = tri.Triangulation(x, y)
# Determine which triangulation edges are boundary edges; these
# correspond to edges that have no neighboring triangle. Store them as
# pairs of (start,end) point indices.
# This is all calculated long-hand to demonstrate the logic. It could
# be simplified by rewriting in a more pythonic way.
boundaries = []
for i in range(len(triang.triangles)):
for j in range(3):
if triang.neighbors[i,j] < 0:
# Triangle edge (i,j) has no neighbor so is a boundary edge.
boundaries.append((triang.triangles[i,j],
triang.triangles[i,(j+1)%3]))
boundaries = np.asarray(boundaries)
# The following line plots the boundary edges.
plt.plot(x[boundaries].T, y[boundaries].T, 'b-')
plt.plot(x, y, 'bo')
plt.show()