5555 from plotnine import watermark
5656 from plotnine ._mpl .gridspec import p9GridSpec
5757 from plotnine ._mpl .layout_manager ._plot_side_space import PlotSideSpaces
58- from plotnine .composition import Compose , inset_element
58+ from plotnine .composition import Compose
5959 from plotnine .coords .coord import coord
6060 from plotnine .facets .facet import facet
6161 from plotnine .typing import DataLike , FigureFormat , MimeBundle
@@ -139,15 +139,17 @@ class ggplot:
139139 """
140140 Drawing zorder for every axes created for this plot
141141
142- The default (``0``) keeps the plot at the base layer. `_draw_insets`
143- raises this on inset plots so they render above their host.
142+ The default (``0``) keeps the plot at the base layer.
143+ `inset_element._setup` raises this on inset plots so they render
144+ above their host.
144145 """
145146
146147 def __init__ (
147148 self ,
148149 data : Optional [DataLike ] = None ,
149150 mapping : Optional [aes ] = None ,
150151 ):
152+ from .composition ._inset_element import Insets
151153 from .mapping ._env import Environment
152154
153155 # Allow some sloppiness
@@ -164,7 +166,7 @@ def __init__(
164166 self .environment = Environment .capture (1 )
165167 self .layout = Layout ()
166168 self .watermarks : list [watermark ] = []
167- self ._insets : list [ inset_element ] = []
169+ self ._insets : Insets = Insets ()
168170
169171 # build artefacts
170172 self ._build_objs = NS (meta = {})
@@ -383,7 +385,7 @@ def draw(self, *, show: bool = False) -> Figure:
383385
384386 # Insets render after host theming is finalised so their
385387 # own draw picks up a fully-realised host figure.
386- self ._draw_insets ()
388+ self ._insets . draw ()
387389
388390 return figure
389391
@@ -392,6 +394,7 @@ def _setup(self) -> Figure:
392394 Setup this instance for the building process
393395 """
394396 self ._create_figure ()
397+ self ._insets ._setup (self )
395398 self .labels .add_defaults (self .mapping .labels )
396399 return self .figure
397400
@@ -602,23 +605,6 @@ def _draw_watermarks(self):
602605 for wm in self .watermarks :
603606 wm .draw (self .figure )
604607
605- def _draw_insets (self ):
606- """
607- Draw every inset attached to this plot into the host figure
608-
609- Each inset reuses the host's figure instead of creating its own.
610- The inset's zorder is raised by `INSET_ZORDER_STEP` so every
611- figure-level artist on the inset sits above the host's; for
612- Compose insets this zorder is propagated down the sub-tree when
613- the composition is drawn.
614- """
615- from .composition ._inset_element import INSET_ZORDER_STEP
616-
617- for inset in self ._insets :
618- inset .obj .figure = self .figure
619- inset .obj ._zorder = self ._zorder + INSET_ZORDER_STEP
620- inset .obj .draw ()
621-
622608 def _add_figure_artist (self , artist ):
623609 """
624610 Add an artist to this plot's figure with the right zorder offset
0 commit comments