Skip to content
Open
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
9 changes: 9 additions & 0 deletions lib/matplotlib/backends/backend_mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ def __getattr__(self, attr):
# to the underlying C implementation).
return getattr(self._renderer, attr)

def close_group(self, s):
# docstring inherited
# If rasterizing can be stopped, stop it before closing the group so
# the vector backend receives the matching close_group (#32174).
if self._raster_depth == 0 and self._rasterizing:
self.stop_rasterizing()
self._rasterizing = False
self._renderer.close_group(s)

def close_blend_group(self):
# docstring inherited
# If rasterizing can be stopped, stop it before closing the group
Expand Down
32 changes: 32 additions & 0 deletions lib/matplotlib/tests/test_backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,38 @@ def count_tag(fig, tag):
assert count_tag(fig5, "path") == 1 # axis patch


def test_rasterized_zorder_axes_hierarchy():
# With rasterized artists ending an Axes draw, MixedModeRenderer must flush
# before close_group so SVG axes groups stay siblings (#32174).
fig, axs = plt.subplots(ncols=2)
for ax in axs:
ax.scatter([1, 2], [1, 2], rasterized=True, zorder=10)

with BytesIO() as fd:
fig.savefig(fd, format='svg')
buf = fd.getvalue().decode()

# Strip the default namespace so ElementTree tag matching stays simple.
buf = buf.replace(' xmlns="http://www.w3.org/2000/svg"', '', 1)
root = xml.etree.ElementTree.fromstring(buf)

axes_parents = {}

def walk(elem, axes_ancestor=None):
gid = elem.get('id') if elem.tag == 'g' else None
next_ancestor = axes_ancestor
if gid is not None and gid.startswith('axes_'):
axes_parents[gid] = axes_ancestor
next_ancestor = gid
for child in elem:
walk(child, next_ancestor)

walk(root)
assert set(axes_parents) >= {'axes_1', 'axes_2'}
assert axes_parents['axes_1'] is None
assert axes_parents['axes_2'] is None


# Use Computer Modern Sans Serif, not Helvetica (which has no \textwon).
@mpl.style.context('default')
@needs_usetex
Expand Down
Loading