Skip to content

Commit d7de3b8

Browse files
committed
feat(layout): honor strip_switch_pad for outside strip placement
1 parent 5fed129 commit d7de3b8

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

plotnine/_mpl/layout_manager/_plot_side_space.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@ def _axis_ticks_and_text(self) -> float:
152152
"""
153153
return self.sum_incl("axis_ticks") - self.sum_upto("axis_text")
154154

155+
def _strip_switch_pad(self, axis: Literal["x", "y"]) -> float:
156+
"""
157+
Reserved gap between a shared axis and a strip placed beyond it
158+
159+
The gap is non-zero only for `strip_placement="outside"` on a
160+
side that carries both a strip and an axis; otherwise zero. The
161+
themeable value is in points and is returned as a fraction of
162+
the relevant figure dimension (height for x strips, width for y).
163+
"""
164+
theme = self.items.plot.theme
165+
if theme.getp("strip_placement") != "outside":
166+
return 0
167+
strip_breadth: float = self.strip_text # pyright: ignore[reportAttributeAccessIssue]
168+
if not (strip_breadth and self._axis_ticks_and_text):
169+
return 0
170+
pad_pt = theme.getp(f"strip_switch_pad_{axis}") or 0
171+
W, H = theme.getp("figure_size")
172+
dim = H if axis == "x" else W
173+
return (pad_pt / 72) / dim
174+
155175
def strip_band_offset(self, member: Literal["strip", "axis"]) -> float:
156176
"""
157177
Outward offset for one member of a shared strip/axis band
@@ -176,8 +196,9 @@ def strip_band_offset(self, member: Literal["strip", "axis"]) -> float:
176196
placement = self.items.plot.theme.getp("strip_placement")
177197
if placement == "inside":
178198
return 0 if member == "strip" else strip_breadth
179-
# "outside"
180-
return axis_breadth if member == "strip" else 0
199+
# "outside": the strip clears the axis, plus the switch pad
200+
pad: float = self.strip_switch_pad # pyright: ignore[reportAttributeAccessIssue]
201+
return axis_breadth + pad if member == "strip" else 0
181202

182203

183204
class left_space(_plot_side_space):
@@ -249,6 +270,8 @@ class left_space(_plot_side_space):
249270
axis_text_margin: float = 0
250271
"""Margin to the right of the y-axis text (panel-facing side)"""
251272
axis_ticks: float = 0
273+
strip_switch_pad: float = 0
274+
"""Gap between a shared axis and a strip beyond it (outside placement)"""
252275
strip_text: float = 0
253276
"""Outward extent of a left facet strip"""
254277

@@ -287,6 +310,7 @@ def _calculate(self):
287310

288311
self.axis_ticks = items.axis_ticks_y_left
289312
self.strip_text = items.strip_text_y("left")
313+
self.strip_switch_pad = self._strip_switch_pad("y")
290314

291315
# Adjust plot_margin to make room for ylabels that protude well
292316
# beyond the axes
@@ -380,6 +404,8 @@ class right_space(_plot_side_space):
380404
axis_text_margin: float = 0
381405
"""Margin to the left of the y-axis text (panel-facing side)"""
382406
axis_ticks: float = 0
407+
strip_switch_pad: float = 0
408+
"""Gap between a shared axis and a strip beyond it (outside placement)"""
383409
strip_text: float = 0
384410
"""Outward extent of a right facet strip (next to the panel by default)"""
385411

@@ -419,6 +445,7 @@ def _calculate(self):
419445
MARGIN_SIDE["right"],
420446
)
421447
self.axis_ticks = items.axis_ticks_y_right
448+
self.strip_switch_pad = self._strip_switch_pad("y")
422449

423450
# Adjust plot_margin to make room for ylabels that protude well
424451
# beyond the axes
@@ -528,6 +555,8 @@ class top_space(_plot_side_space):
528555
axis_text_margin: float = 0
529556
"""Margin below the x-axis text (panel-facing side)"""
530557
axis_ticks: float = 0
558+
strip_switch_pad: float = 0
559+
"""Gap between a shared axis and a strip beyond it (outside placement)"""
531560
strip_text: float = 0
532561
"""Outward extent of a top facet strip (next to the panel by default)"""
533562

@@ -580,6 +609,7 @@ def _calculate(self):
580609
MARGIN_SIDE["top"],
581610
)
582611
self.axis_ticks = items.axis_ticks_x_top
612+
self.strip_switch_pad = self._strip_switch_pad("x")
583613

584614
# Adjust plot_margin to make room for ylabels that protude well
585615
# beyond the axes
@@ -700,6 +730,8 @@ class bottom_space(_plot_side_space):
700730
axis_text_margin: float = 0
701731
"""Margin above the x-axis text (panel-facing side)"""
702732
axis_ticks: float = 0
733+
strip_switch_pad: float = 0
734+
"""Gap between a shared axis and a strip beyond it (outside placement)"""
703735
strip_text: float = 0
704736
"""Outward extent of a bottom facet strip"""
705737

@@ -751,6 +783,7 @@ def _calculate(self):
751783
)
752784
self.axis_ticks = items.axis_ticks_x_bottom
753785
self.strip_text = items.strip_text_x("bottom")
786+
self.strip_switch_pad = self._strip_switch_pad("x")
754787

755788
# Adjust plot_margin to make room for ylabels that protude well
756789
# beyond the axes

0 commit comments

Comments
 (0)