Untitled
unknown
python
2 years ago
3.3 kB
8
Indexable
from matplotlib.path import Path from matplotlib.patches import PathPatch def interpolate_points(ax, x, y, pad, open=True, absolute_pad=False, **kwargs): codes = [] verts = [] if open: # First point code = Path.MOVETO vert = (x[0], y[0]) codes.append(code) verts.append(vert) # Interpolate points between for i in range(1, len(x)-1): # LINETO (from previous to this point) code = Path.LINETO dx = (x[i] - x[i-1]) * (1 - pad) dy = (y[i] - y[i-1]) * (1 - pad) xij = x[i-1] + dx yij = y[i-1] + dy vert = (xij, yij) codes.append(code) verts.append(vert) # CURVETO code = Path.CURVE3 xij = x[i] yij = y[i] vert = (xij, yij) codes.append(code) verts.append(vert) # CURVETO code = Path.CURVE3 xij = x[i] + (x[i+1] - x[i]) * pad yij = y[i] + (y[i+1] - y[i]) * pad vert = (xij, yij) codes.append(code) verts.append(vert) # Last point code = Path.LINETO vert = (x[-1], y[-1]) codes.append(code) verts.append(vert) else: # First point code = Path.MOVETO xij = x[0] + (x[1] - x[0]) * pad yij = y[0] + (y[1] - y[0]) * pad vert = (xij, yij) codes.append(code) verts.append(vert) # Interpolate points between for i in range(1, len(x)-1): # LINETO code = Path.LINETO xij = x[i-1] + (x[i] - x[i-1]) * (1 - pad) yij = y[i-1] + (y[i] - y[i-1]) * (1 - pad) vert = (xij, yij) codes.append(code) verts.append(vert) # CURVETO code = Path.CURVE3 xij = x[i] yij = y[i] vert = (xij, yij) codes.append(code) verts.append(vert) # CURVETO code = Path.CURVE3 xij = x[i] + (x[i+1] - x[i]) * pad yij = y[i] + (y[i+1] - y[i]) * pad vert = (xij, yij) codes.append(code) verts.append(vert) # Last point code = Path.LINETO xij = x[-2] + (x[-1] - x[-2]) * (1 - pad) yij = y[-2] + (y[-1] - y[-2]) * (1 - pad) vert = (xij, yij) codes.append(code) verts.append(vert) # CURVETO code = Path.CURVE3 xij = x[-1] yij = y[-1] vert = (xij, yij) codes.append(code) verts.append(vert) # CURVETO code = Path.CURVE3 xij = x[-1] + (x[0] - x[-1]) * pad yij = y[-1] + (y[0] - y[-1]) * pad vert = (xij, yij) codes.append(code) verts.append(vert) # Back to first point code = Path.LINETO xij = x[-1] + (x[0] - x[-1]) * (1 - pad) yij = y[-1] + (y[0] - y[-1]) * (1 - pad) vert = (xij, yij) codes.append(code) verts.append(vert) # CURVETO code = Path.CURVE3 xij = x[0] yij = y[0] vert = (xij, yij) codes.append(code) verts.append(vert) # CURVETO code = Path.CURVE3 xij = x[0] + (x[1] - x[0]) * pad yij = y[0] + (y[1] - y[0]) * pad vert = (xij, yij) codes.append(code) verts.append(vert) rounded_verts = Path(verts, codes) rounded_verts = PathPatch(rounded_verts, **kwargs) ax.add_patch(rounded_verts) fig = matplotlib.pyplot.figure(dpi=220) box = fig.hbox(1) ax = box.children[0] x = np.array([0.25, 0.25, 0.75, 0.75]) y = np.array([0, 1, 1, 0]) interpolate_points(ax, x, y, 0.2, open=False, facecolor=dv.colors['green'][2], edgecolor=dv.green)
Editor is loading...