Skip to content

Instantly share code, notes, and snippets.

@moorepants
Created March 4, 2015 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moorepants/4cac02e798446bb46de7 to your computer and use it in GitHub Desktop.
Save moorepants/4cac02e798446bb46de7 to your computer and use it in GitHub Desktop.
Trying to port SVG Bezier paths to matplotlib
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
s = "m 369.01724,467.04725 c -0.18526,2.57511 0.025,3.71002 -0.74575,6.17343 -1.53222,4.89942 -7.46833,7.60079 -8.99133,12.50302 -1.75723,5.65693 -0.76258,11.95174 0.37357,17.76566 1.34911,6.90236 5.61166,12.84732 7.04916,19.73262 0.66711,3.19499 0.60249,6.59014 -0.27085,9.73526 -0.9587,3.45198 -3.089,5.23513 -4.4528,8.54866 -1.21939,2.96186 -2.15924,6.50136 -1.66722,9.6668 0.55788,3.58921 2.31624,7.05338 4.75882,9.74186 0.76564,0.84272 2.2695,1.96683 3.33049,2.37969 2.95478,1.1512 6.02208,1.86489 9.19306,1.87852 3.3538,0.0148 6.69086,-0.81148 9.68045,-2.33079 1.6753,-0.85214 2.95914,-3.06572 3.55483,-4.84835 1.59252,-4.76535 -0.99967,-9.16932 -1.47916,-14.171 -0.65213,-6.80233 -2.15122,-13.65888 -1.41097,-20.45291 0.4785,-4.39107 3.41465,-8.30725 3.73802,-12.71261 0.23998,-3.26968 -0.68483,-6.32005 -0.14903,-9.55505 0.32782,-1.97814 1.27144,-3.56144 1.49529,-5.55522 0.30253,-2.69162 -0.67713,-5.5023 -1.96412,-7.88686 -2.07288,-3.84076 -7.12129,-5.62393 -8.88424,-9.61605 -1.56515,-3.54434 -3.5489,-8.66548 -0.99668,-11.58071 1.30013,-1.48461 4.86591,0.81081 6.05286,-0.76661 1.4673,-1.94944 -0.0302,-2.59307 -0.26097,-3.86285 -0.15478,-0.85184 0.9955,-1.22785 1.1535,-2.07814 0.1668,-0.89921 -1.12519,-2.84699 -0.34651,-3.32528 0.78665,-0.48407 2.42659,-0.21032 2.76644,-1.0691 0.52705,-1.33211 -1.94401,-2.19113 -2.44478,-3.53344 -1.70189,-4.56088 0.38516,-7.87799 -2.35425,-11.90105 -1.83474,-2.6953 -4.74134,-3.72263 -7.59265,-4.09935 -3.67521,-0.48697 -7.90368,0.27205 -10.82364,2.55664 -2.75878,2.15848 -4.5135,5.85352 -4.80724,9.34345 -0.29644,3.52345 1.40837,7.06666 3.2078,10.10961 0.76695,1.29754 1.96258,2.19026 2.4078,3.63001 0.4355,1.40789 0.98583,4.11034 0.88009,5.58014 z"
coordinates = []
codes = []
for pair in s.split(' '):
if pair == 'm':
code = Path.MOVETO
elif pair == 'c':
code = Path.CURVE4
try:
x_p, y_p = coordinates[-1]
except IndexError:
x_p, y_p = 0.0, 0.0
try:
dx, dy = map(float, pair.split(','))
except ValueError:
pass
else:
coordinates.append((x_p + dx, y_p - dy))
codes.append(code)
coordinates.append(coordinates[0])
codes.append(Path.CLOSEPOLY)
path = Path(coordinates, codes)
fig, ax = plt.subplots()
patch = patches.PathPatch(path, facecolor='blue', lw=1)
ax.add_patch(patch)
ax.set_xlim((300.0, 600.0))
ax.set_ylim((400.0, 700.0))
x, y = zip(*path.vertices)
line, = ax.plot(x, y, 'go-')
ax.grid()
ax.axis('equal')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment