Skip to content

Commit dd03983

Browse files
committed
Use Insets container class for ggplot._insets
1 parent a817959 commit dd03983

2 files changed

Lines changed: 41 additions & 22 deletions

File tree

plotnine/composition/_inset_element.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,42 @@ def __post_init__(self):
7575
f"bottom={self.bottom!r}, top={self.top!r}."
7676
)
7777

78+
def _setup(self, parent: ggplot):
79+
"""
80+
Receive the host figure and zorder from parent
81+
"""
82+
self.obj.figure = parent.figure
83+
self.obj._zorder = parent._zorder + INSET_ZORDER_STEP
84+
85+
def draw(self):
86+
"""
87+
Render this inset
88+
"""
89+
self.obj.draw()
90+
7891
def __radd__(self, other: ggplot) -> ggplot:
7992
"""
8093
Attach this inset to a ggplot
8194
"""
8295
other._insets.append(deepcopy(self))
8396
return other
97+
98+
99+
class Insets(list[inset_element]):
100+
"""
101+
List of insets attached to a ggplot
102+
"""
103+
104+
def _setup(self, parent: ggplot):
105+
"""
106+
Receive the host figure and zorder for every inset
107+
"""
108+
for inset in self:
109+
inset._setup(parent)
110+
111+
def draw(self):
112+
"""
113+
Render every inset attached to the host
114+
"""
115+
for inset in self:
116+
inset.draw()

plotnine/ggplot.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
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

Comments
 (0)