Skip to content
Closed
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions lib/matplotlib/backends/backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,13 @@ def draw_path(self, gc, path, transform, rgbFace=None):
# docstring inherited
trans_and_flip = self._make_flip_transform(transform)
clip = (rgbFace is None and gc.get_hatch_path() is None)
simplify = path.should_simplify and clip
# Also simplify large filled paths (e.g. fill_between); see #22803.
# Lines already simplify via should_simplify; honor the rcParam and
# leave hatched paths exact.
simplify = ((path.should_simplify
or (mpl.rcParams['path.simplify']
and len(path.vertices) >= 128))
and gc.get_hatch_path() is None)
path_data = self._convert_path(
path, trans_and_flip, clip=clip, simplify=simplify,
sketch=gc.get_sketch_params())
Expand All @@ -714,7 +720,9 @@ def draw_markers(
path_data = self._convert_path(
marker_path,
marker_trans + Affine2D().scale(1.0, -1.0),
simplify=False)
simplify=(marker_path.should_simplify
or (mpl.rcParams['path.simplify']
and len(marker_path.vertices) >= 128)))
style = self._get_style_dict(gc, rgbFace)
dictkey = (path_data, _generate_css(style))
oid = self._markers.get(dictkey)
Expand Down
22 changes: 22 additions & 0 deletions lib/matplotlib/tests/test_backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,3 +705,25 @@ def test_svgid():

assert plt.rcParams['svg.id'] == svg_id
assert tree.findall(f'.[@id="{svg_id}"]')


def test_fill_between_svg_simplified():
# Regression test for #22803: the polygon produced by fill_between is cached
# in the SVG backend like a reusable marker and was written without path
# simplification, producing very large files. It should be simplified like any
# other large path, while staying vector (not rasterized).
import re
rng = np.random.default_rng(424242)
n = 20000
x = np.arange(n)
y = rng.normal(size=(10, n))
fig, ax = plt.subplots()
ax.fill_between(x, y.min(axis=0), y.max(axis=0))
ax.set_xlim(0, n)
buf = BytesIO()
fig.savefig(buf, format="svg")
svg = buf.getvalue().decode("utf-8")
biggest = max(re.findall(r'\sd="([^"]*)"', svg, re.S), key=len)
n_verts = biggest.count("L") + biggest.count("M")
assert n_verts < n, f"fill path not simplified ({n_verts} vertices)"
assert "<image" not in svg
Loading