Skip to content

Commit 208068e

Browse files
committed
Give each inset its own zorder band
Sibling insets all received the same _zorder (host._zorder + INSET_ZORDER_STEP), so their figure-level artists collided inside one band: an earlier inset's panel_border, titles, axis titles and legend sat above a later inset's panel because they had larger numeric zorders than the panel itself. The "later inset fully covers earlier" intent only held for the panel area, where equal-zorder ties broke by insertion order. inset_element._setup now takes the 1-based index among its siblings and assigns _zorder = parent._zorder + index * INSET_ZORDER_STEP, so each inset occupies its own band. Insets._setup enumerates and passes the index through. INSET_ZORDER_STEP shrinks from 1000 to 10 now that nothing inside a single plot reaches 99.9 anymore: watermark drops from 99.9 to 9, and the explicit zorder=99.1 on the legend's FlexibleAnchoredOffsetbox is removed (the mpl default of 5 is already above all decorations and below the watermark, and ggplot._add_figure_artist offsets it into the right band). The 0.5 gap between an inset's watermark (band top) and the next inset's plot_background (band bottom, -0.5) is the tightest the geometry permits and is safe — no other artist falls in it.
1 parent 59a341b commit 208068e

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

plotnine/composition/_inset_element.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
from ._compose import Compose
1010

1111

12-
INSET_ZORDER_STEP = 1000
12+
INSET_ZORDER_STEP = 10
1313
"""
14-
Zorder added to the host's value when an inset is drawn, so every
15-
figure-level artist on the inset (axes, plot_background, titles,
16-
strip text, legends, ...) sits above every figure-level artist on the
17-
host. Must exceed the largest existing zorder used inside a single
18-
plot — watermarks at 99.9 — so the host and inset stacks never overlap.
14+
Width of the zorder band reserved for each inset
15+
16+
The Nth sibling inset is drawn at `host._zorder + N * INSET_ZORDER_STEP`,
17+
so every figure-level artist on a later inset (axes, plot_background,
18+
titles, strip text, legends, ...) sits above every figure-level artist
19+
on an earlier inset and the host. The step must exceed the largest
20+
within-plot figure-level zorder — watermarks at 9 — by enough that the
21+
next band's lowest artist (`plot_background` at -0.5) still clears it.
1922
"""
2023

2124

@@ -75,12 +78,16 @@ def __post_init__(self):
7578
f"bottom={self.bottom!r}, top={self.top!r}."
7679
)
7780

78-
def _setup(self, parent: ggplot):
81+
def _setup(self, parent: ggplot, index: int):
7982
"""
8083
Receive the host figure and zorder from parent
84+
85+
`index` is the 1-based position of this inset among its siblings.
86+
Each sibling occupies its own zorder band so a later inset's
87+
figure-level artists all sit above an earlier inset's.
8188
"""
8289
self.obj.figure = parent.figure
83-
self.obj._zorder = parent._zorder + INSET_ZORDER_STEP
90+
self.obj._zorder = parent._zorder + index * INSET_ZORDER_STEP
8491

8592
def draw(self):
8693
"""
@@ -105,8 +112,8 @@ def _setup(self, parent: ggplot):
105112
"""
106113
Receive the host figure and zorder for every inset
107114
"""
108-
for inset in self:
109-
inset._setup(parent)
115+
for i, inset in enumerate(self, start=1):
116+
inset._setup(parent, i)
110117

111118
def draw(self):
112119
"""

plotnine/guides/guides.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ def _anchored_offset_box(boxes: list[PackerBase]):
321321
bbox_to_anchor=(0, 0),
322322
bbox_transform=self.plot.figure.transFigure,
323323
borderpad=0.0,
324-
zorder=99.1,
325324
)
326325

327326
# Group together guides for each position

plotnine/watermark.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
__all__ = ("watermark",)
1717

1818

19-
_BASE_ZORDER = 99.9
19+
_BASE_ZORDER = 9
2020
"""
2121
Default zorder for a watermark on a top-level plot
2222
2323
Plotnine manages the zorder of every figure-level artist so that insets
24-
stack predictably above their host.
24+
stack predictably above their host. This value must stay below
25+
`INSET_ZORDER_STEP - 0.5` so a sibling inset's `plot_background`
26+
(at `_zorder - 0.5`) clears the watermark of the inset below it.
2527
"""
2628

2729

0 commit comments

Comments
 (0)