[Bug]: Using `rasterized=True` with `zorder` makes the generated SVG have wrong axes layer hierarchy · Issue #32174 · matplotlib/matplotlib · GitHub
Skip to content

[Bug]: Using rasterized=True with zorder makes the generated SVG have wrong axes layer hierarchy #32174

Closed
SouravVerma-art/matplotlib
#1

Description

@ahmedselim2017

Bug summary

Hi, while using rasterized=True with the zorder, the saved svg files have a wrong axes layer hierarchy:

However, interestingly if another drawing command is called after such as ax.scatter(x=[], y=[], rasterized=False, zorder=10) with the same zorder but without rasterization, the generated svg file has correct layer hierarchy.

Code for reproduction

import matplotlib.pyplot as plt

fig, axs = plt.subplots(ncols=2)
for i, ax in enumerate(axs):
    ax.scatter(x=[1, 2], y=[1, 2], rasterized=True, zorder=10)
fig.savefig("minimal_not_working.svg")

fig, axs = plt.subplots(ncols=2)
for i, ax in enumerate(axs):
    ax.scatter(x=[1, 2], y=[1, 2], rasterized=True, zorder=10)
    ax.scatter(x=[], y=[], rasterized=False, zorder=10)
fig.savefig("minimal_working.svg")

Actual outcome

Axes layer hierarchy of minimal_not_working.svg:

- figure_1
  - axes_1
    - [ parts of the axes_1 ] 
    - axes_2
      - [ parts of the axes_2 ] 

Axes layer hierarchy of minimal_working.svg:

- figure_1
  - axes_1
    - [ parts of the axes_1 ] 
  - axes_2
    - [ parts of the axes_2 ] 

Expected outcome

The minimal_not_working.svg should have an axes layer hierarchy simlar to:

- figure_1
  - axes_1
    - [ parts of the axes_1 ] 
  - axes_2
    - [ parts of the axes_2 ] 

Additional information

No response

Operating system

No response

Matplotlib Version

3.11.1

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Activity

  1. SouravVerma-art commented on Aug 6, 2026

    @SouravVerma-art

    hii @ahmedselim2017 can you assign this issue to me

  2. SouravVerma-art commented on Aug 6, 2026

    @SouravVerma-art

    I'd like to work on this. I'll look into backend_svg.py and the rasterization group handling

  3. tacaswell commented on Aug 6, 2026

    @tacaswell
    Member

    We do not assign issues.

    My guess is that this is related to an optimization to reduce the total number of raster images in the output, but to be clear that is a guess without having looked at the code in question recently.

  4. QuLogic commented on Aug 15, 2026

    @QuLogic
    Member

    @SouravVerma-art you seem to have opened and merged a PR to your own fork, but not here; were you intending to open one here?

  5. ShamikOfficial commented on Sep 7, 2026

    @ShamikOfficial
    Contributor

    Looking into this. I'll start from the SVG backend rasterization grouping and try to keep sibling axes from nesting when rasterized=True is used with zorder.

  6. SouravVerma-art commented on Sep 23, 2026

    @SouravVerma-art

    Thanks, for pointing that out. Yes I did mean to contribute the fix upstream. I started by opening the request on my fork while I was testing the changes.

    I also noticed that issue #32311 is now linked to this one. I will look at that approach. Compare it with my own changes before I open a pull request upstream. That way I can make sure I’m not doing work.

  7. added a commit that references this issue on Oct 20, 2026
    ac0c839
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions