Skip to content

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

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

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