Skip to content

Commit 962dceb

Browse files
committed
refactor(layout): compute strip patch geometry in figure space via ArtistGeometry
1 parent 315486e commit 962dceb

1 file changed

Lines changed: 15 additions & 21 deletions

File tree

plotnine/_mpl/layout_manager/_plot_layout_items.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,6 @@ def axis_ticks_y(self, ax: Axes) -> Iterator[Tick]:
219219

220220
return chain(major, minor)
221221

222-
@property
223-
def _renderer(self):
224-
"""
225-
Renderer for the current figure
226-
"""
227-
return self.plot.figure._get_renderer() # pyright: ignore[reportAttributeAccessIssue]
228-
229222
def _strip_sizing(self, position: StripPosition) -> StripSizing:
230223
"""
231224
Theme inputs that fix one strip's background size and offset
@@ -256,7 +249,7 @@ def strip_patch_bbox(
256249
self, strip_text: StripText, scale: float = 1
257250
) -> Bbox:
258251
"""
259-
Display-space bounding box of one strip's background patch
252+
Figure-space bounding box of one strip's background patch
260253
261254
The breadth (height for top strips, width for right strips) is
262255
scaled by `scale` so the layout manager can equalise strips in
@@ -266,20 +259,23 @@ def strip_patch_bbox(
266259

267260
sizing = self._strip_sizing(strip_text.position)
268261
m = sizing.margin
269-
text_bbox = strip_text.get_window_extent(self._renderer)
270-
ax_bbox = strip_text.ax.bbox.frozen()
271-
line_height = strip_text._line_height(self._renderer)
262+
text_bbox = self.geometry.bbox(strip_text)
263+
ax_bbox = self.geometry.bbox(strip_text.ax)
264+
W, H = self.plot.figure.bbox.width, self.plot.figure.bbox.height
265+
line_height = strip_text._line_height(self.geometry.renderer)
272266

273267
x0 = rel_position(sizing.bg_x, 0, ax_bbox.x0, ax_bbox.x1)
274268
y0 = rel_position(sizing.bg_y, 0, ax_bbox.y0, ax_bbox.y1)
275269

276270
if strip_text.position == "top":
271+
margins = (m.b + m.t) * line_height / H
277272
width = ax_bbox.width * sizing.bg_width
278-
height = (text_bbox.height + (m.b + m.t) * line_height) * scale
273+
height = (text_bbox.height + margins) * scale
279274
y0 += height * sizing.strip_align
280275
else:
276+
margins = (m.l + m.r) * line_height / W
281277
height = ax_bbox.height * sizing.bg_height
282-
width = (text_bbox.width + (m.l + m.r) * line_height) * scale
278+
width = (text_bbox.width + margins) * scale
283279
x0 += width * sizing.strip_align
284280
return Bbox.from_bounds(x0, y0, width, height)
285281

@@ -290,16 +286,15 @@ def strip_text_x_extra_height(self, position: StripPosition) -> float:
290286
if not self.strip_text_x:
291287
return 0
292288

293-
fig_height = self.plot.figure.bbox.height
294289
heights = []
295290
for st in self.strip_text_x:
296291
if st.position != position:
297292
continue
298293
strip_align = self._strip_sizing(st.position).strip_align
299294
if st.patch.get_visible():
300295
# The patch bounds are not yet set, so derive its natural
301-
# height directly in figure space.
302-
h = self.strip_patch_bbox(st).height / fig_height
296+
# height from the sizing inputs.
297+
h = self.strip_patch_bbox(st).height
303298
else:
304299
h = self.geometry.height(st)
305300
heights.append(max(h + h * strip_align, 0))
@@ -313,16 +308,15 @@ def strip_text_y_extra_width(self, position: StripPosition) -> float:
313308
if not self.strip_text_y:
314309
return 0
315310

316-
fig_width = self.plot.figure.bbox.width
317311
widths = []
318312
for st in self.strip_text_y:
319313
if st.position != position:
320314
continue
321315
strip_align = self._strip_sizing(st.position).strip_align
322316
if st.patch.get_visible():
323317
# The patch bounds are not yet set, so derive its natural
324-
# width directly in figure space.
325-
w = self.strip_patch_bbox(st).width / fig_width
318+
# width from the sizing inputs.
319+
w = self.strip_patch_bbox(st).width
326320
else:
327321
w = self.geometry.width(st)
328322
widths.append(max(w + w * strip_align, 0))
@@ -667,7 +661,7 @@ def _place_strip_backgrounds(self):
667661
for st, scale in zip(group, scales):
668662
bbox = self.strip_patch_bbox(st, scale)
669663
st.patch.set_bounds(bbox.bounds)
670-
st.patch.set_transform(None)
664+
st.patch.set_transform(self.plot.figure.transFigure)
671665
self._place_strip_text(st)
672666

673667
def _place_strip_text(self, st: StripText):
@@ -677,7 +671,7 @@ def _place_strip_text(self, st: StripText):
677671
theme = self.plot.theme
678672
position = st.position
679673
ax = st.ax
680-
renderer = self._renderer
674+
renderer = self.geometry.renderer
681675
sizing = self._strip_sizing(position)
682676
m = sizing.margin
683677

0 commit comments

Comments
 (0)