Skip to content

Commit a519dea

Browse files
committed
Account for both axis_text margins
Previously only the margin between the text and the ticks was accounted for in the layout. Now the margin between the text and axis title works.
1 parent e71a8bb commit a519dea

File tree

5 files changed

+16
-15
lines changed

5 files changed

+16
-15
lines changed

plotnine/_mpl/layout_manager/_layout_items.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,8 @@ def axis_text_x_max_height(self, ax: Axes) -> float:
342342
"""
343343
Return maximum height[figure space] of x tick labels
344344
"""
345-
top_margin = (
346-
0
347-
if self._is_blank("axis_text_x")
348-
else self.plot.theme.get_margin("axis_text_x").fig.t
349-
)
350345
heights = [
351-
self.calc.tight_height(label) + top_margin
352-
for label in self.axis_text_x(ax)
346+
self.calc.tight_height(label) for label in self.axis_text_x(ax)
353347
]
354348
return max(heights) if len(heights) else 0
355349

@@ -378,14 +372,8 @@ def axis_text_y_max_width(self, ax: Axes) -> float:
378372
"""
379373
Return maximum width[figure space] of y tick labels
380374
"""
381-
right_margin = (
382-
0
383-
if self._is_blank("axis_text_y")
384-
else self.plot.theme.get_margin("axis_text_y").fig.r
385-
)
386375
widths = [
387-
self.calc.tight_width(label) + right_margin
388-
for label in self.axis_text_y(ax)
376+
self.calc.tight_width(label) for label in self.axis_text_y(ax)
389377
]
390378
return max(widths) if len(widths) else 0
391379

plotnine/_mpl/layout_manager/_spaces.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ class left_spaces(_side_spaces):
222222
axis_title_y_margin_left: float = 0
223223
axis_title_y: float = 0
224224
axis_title_y_margin_right: float = 0
225+
axis_text_y_margin_left: float = 0
225226
axis_text_y: float = 0
227+
axis_text_y_margin_right: float = 0
226228
axis_ticks_y: float = 0
227229

228230
def _calculate(self):
@@ -256,6 +258,11 @@ def _calculate(self):
256258

257259
# Account for the space consumed by the axis
258260
self.axis_text_y = items.axis_text_y_max_width_at("first_col")
261+
if self.axis_text_y:
262+
m = theme.get_margin("axis_text_y").fig
263+
self.axis_text_y_margin_left = m.l
264+
self.axis_text_y_margin_right = m.r
265+
259266
self.axis_ticks_y = items.axis_ticks_y_max_width_at("first_col")
260267

261268
# Adjust plot_margin to make room for ylabels that protude well
@@ -576,7 +583,9 @@ class bottom_spaces(_side_spaces):
576583
axis_title_x_margin_bottom: float = 0
577584
axis_title_x: float = 0
578585
axis_title_x_margin_top: float = 0
586+
axis_text_x_margin_bottom: float = 0
579587
axis_text_x: float = 0
588+
axis_text_x_margin_top: float = 0
580589
axis_ticks_x: float = 0
581590

582591
def _calculate(self):
@@ -616,8 +625,12 @@ def _calculate(self):
616625
self.axis_title_x_margin_top = m.t * F
617626

618627
# Account for the space consumed by the axis
619-
self.axis_ticks_x = items.axis_ticks_x_max_height_at("last_row")
620628
self.axis_text_x = items.axis_text_x_max_height_at("last_row")
629+
if self.axis_text_x:
630+
m = theme.get_margin("axis_text_x").fig
631+
self.axis_text_x_margin_bottom = m.b
632+
self.axis_text_x_margin_top = m.t
633+
self.axis_ticks_x = items.axis_ticks_x_max_height_at("last_row")
621634

622635
# Adjust plot_margin to make room for ylabels that protude well
623636
# beyond the axes
Loading
Loading
Loading

0 commit comments

Comments
 (0)