Skip to content

[Bug]: close_group is not called when using patheffects #27843

Closed
@sherlockdoyle

Description

@sherlockdoyle

Bug summary

I am in the process of developing a custom Renderer for matplotlib. My understanding is that every Renderer.open_group call will have a corresponding Renderer.close_group call. However, when patheffects are used (at least), the close_group never gets called.

Code for reproduction

from matplotlib import patheffects
from matplotlib.backend_bases import FigureCanvasBase, GraphicsContextBase, RendererBase
from matplotlib.figure import Figure
from matplotlib.path import Path
from matplotlib.transforms import Transform
from matplotlib.typing import ColorType


class Renderer(RendererBase):
    def __init__(self) -> None:
        super().__init__()
        self.indent = ''

    def open_group(self, s: str, gid: str | None = None) -> None:
        print(self.indent + 'open ' + s)
        self.indent += '  '
        return super().open_group(s, gid)

    def close_group(self, s: str) -> None:
        self.indent = self.indent[:-2]
        print(self.indent + 'close ' + s)
        return super().close_group(s)

    def draw_path(
        self, gc: GraphicsContextBase, path: Path, transform: Transform, rgbFace: ColorType | None = None
    ) -> None:
        pass


class Canvas(FigureCanvasBase):
    def draw(self):
        self.figure.draw(Renderer())


figure = Figure()
canvas = Canvas(figure)
ax = figure.gca()
ax.set_axis_off()

cg1 = ax.contour([[0, 1], [2, 3]])
cg1.set(path_effects=[patheffects.withTickedStroke(angle=135)])

canvas.draw()

Actual outcome

open figure
  open patch
  close patch
  open axes
    open QuadContourSet
    close axes
  close figure

Expected outcome

QuadContourSet is closed.

open figure
  open patch
  close patch
  open axes
    open QuadContourSet
    close QuadContourSet
  close axes
close figure

Additional information

After some investigation, it appears that in Collection.draw, if any path effects are present, the renderer is updated to PathEffectRenderer. Subsequently, close_group is called on this updated renderer, but this call does not reach the original renderer.

Operating system

Ubuntu

Matplotlib Version

3.8.3

Matplotlib Backend

Custom

Python version

Python 3.10.12

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions