8000 Merge pull request #18981 from anntzer/ffsvg · matplotlib/matplotlib@702cdbc · GitHub
[go: up one dir, main page]

Skip to content

Commit 702cdbc

Browse files
authored
Merge pull request #18981 from anntzer/ffsvg
Cleanup Firefox SVG example.
2 parents e097bf4 + 0b92b4f commit 702cdbc

File tree

1 file changed

+23
-35
lines changed

1 file changed

+23
-35
lines changed

examples/showcase/firefox.py

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,38 @@ def svg_parse(path):
2222
'Q': (Path.CURVE3,)*2,
2323
'C': (Path.CURVE4,)*3,
2424
'Z': (Path.CLOSEPOLY,)}
25-
path_re = re.compile(r'([MLHVCSQTAZ])([^MLHVCSQTAZ]+)', re.IGNORECASE)
26-
float_re = re.compile(r'(?:[\s,]*)([+-]?\d+(?:\.\d+)?)')
2725
vertices = []
2826
codes = []
29-
last = (0, 0)
30-
for cmd, values in path_re.findall(path):
31-
points = [float(v) for v in float_re.findall(values)]
32-
points = np.array(points).reshape((len(points)//2, 2))
27+
cmd_values = re.split("([A-Za-z])", path)[1:] # Split over commands.
28+
for cmd, values in zip(cmd_values[::2], cmd_values[1::2]):
29+
# Numbers are separated either by commas, or by +/- signs (but not at
30+
# the beginning of the string).
31+
points = ([*map(float, re.split(",|(?<!^)(?=[+-])", values))] if values
32+
else [(0., 0.)]) # Only for "z/Z" (CLOSEPOLY).
33+
points = np.reshape(points, (-1, 2))
3334
if cmd.islower():
34-
points += last
35-
cmd = cmd.capitalize()
36-
last = points[-1]
37-
codes.extend(commands[cmd])
38-
vertices.extend(points.tolist())
39-
return codes, vertices
35+
points += vertices[-1][-1]
36+
codes.extend(commands[cmd.upper()])
37+
vertices.append(points)
38+
return np.array(codes), np.concatenate(vertices)
4039

41-
# SVG to matplotlib
40+
41+
# SVG to Matplotlib
4242
codes, verts = svg_parse(firefox)
43-
verts = np.array(verts)
4443
path = Path(verts, codes)
4544

46-
# Make upside down
47-
verts[:, 1] *= -1
48-
xmin, xmax = verts[:, 0].min()-1, verts[:, 0].max()+1
49-
ymin, ymax = verts[:, 1].min()-1, verts[:, 1].max()+1
45+
xmin, ymin = verts.min(axis=0) - 1
46+
xmax, ymax = verts.max(axis=0) + 1
5047

51-
fig = plt.figure(figsize=(5, 5))
52-
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False, aspect=1)
48+
fig = plt.figure(figsize=(5, 5), facecolor="0.75") # gray background
49+
ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1,
50+
xlim=(xmin, xmax), # centering
51+
ylim=(ymax, ymin), # centering, upside down
52+
xticks=[], yticks=[]) # no ticks
5353

5454
# White outline (width = 6)
55-
patch = patches.PathPatch(path, facecolor='None', edgecolor='w', lw=6)
56-
ax.add_patch(patch)
57-
55+
ax.add_patch(patches.PathPatch(path, facecolor='none', edgecolor='w', lw=6))
5856
# Actual shape with black outline
59-
patch = patches.PathPatch(path, facecolor='orange', edgecolor='k', lw=2)
60-
ax.add_patch(patch)
61-
62-
# Centering
63-
ax.set_xlim(xmin, xmax)
64-
ax.set_ylim(ymin, ymax)
65-
66-
# No ticks
67-
ax.set_xticks([])
68-
ax.set_yticks([])
57+
ax.add_patch(patches.PathPatch(path, facecolor='orange', edgecolor='k', lw=2))
6958

70-
# Display
71-
plt.show()
59+
plt.show() # Display

0 commit comments

Comments
 (0)
0