8000 Cleanup Firefox SVG example. by anntzer · Pull Request #18981 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Cleanup Firefox SVG example. #18981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 23 additions & 35 deletions examples/showcase/firefox.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,38 @@ def svg_parse(path):
'Q': (Path.CURVE3,)*2,
'C': (Path.CURVE4,)*3,
'Z': (Path.CLOSEPOLY,)}
path_re = re.compile(r'([MLHVCSQTAZ])([^MLHVCSQTAZ]+)', re.IGNORECASE)
float_re = re.compile(r'(?:[\s,]*)([+-]?\d+(?:\.\d+)?)')
vertices = []
codes = []
last = (0, 0)
for cmd, values in path_re.findall(path):
points = [float(v) for v in float_re.findall(values)]
points = np.array(points).reshape((len(points)//2, 2))
cmd_values = re.split("([A-Za-z])", path)[1:] # Split over commands.
for cmd, values in zip(cmd_values[::2], cmd_values[1::2]):
# Numbers are separated either by commas, or by +/- signs (but not at
# the beginning of the string).
points = ([*map(float, re.split(",|(?<!^)(?=[+-])", values))] if values
else [(0., 0.)]) # Only for "z/Z" (CLOSEPOLY).
points = np.reshape(points, (-1, 2))
if cmd.islower():
points += last
cmd = cmd.capitalize()
last = points[-1]
codes.extend(commands[cmd])
vertices.extend(points.tolist())
return codes, vertices
points += vertices[-1][-1]
codes.extend(commands[cmd.upper()])
vertices.append(points)
return np.array(codes), np.concatenate(vertices)

# SVG to matplotlib

# SVG to Matplotlib
codes, verts = svg_parse(firefox)
verts = np.array(verts)
path = Path(verts, codes)

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

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

# White outline (width = 6)
patch = patches.PathPatch(path, facecolor='None', edgecolor='w', lw=6)
ax.add_patch(patch)

ax.add_patch(patches.PathPatch(path, facecolor='none', edgecolor='w', lw=6))
# Actual shape with black outline
patch = patches.PathPatch(path, facecolor='orange', edgecolor='k', lw=2)
ax.add_patch(patch)

# Centering
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)

# No ticks
ax.set_xticks([])
ax.set_yticks([])
ax.add_patch(patches.PathPatch(path, facecolor='orange', edgecolor='k', lw=2))

# Display
plt.show()
plt.show() # Display
0