Skip to content

Commit 1a61a9b

Browse files
committed
Make API of get_tightbbox more consistent between Axes and Axis.
Instead of having Axes trying to figure out what direction to ignore, let the Axis handle that. This keeps the signature of get_tightbbox consistent between Axes and Axis. Also fix a typo bb_xaxis -> bb_yaxis.
1 parent 67a987e commit 1a61a9b

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

doc/api/api_changes_3.3/behaviour.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ x direction, making the axes smaller in the x-direction doesn't help. The
223223
behavior of both has been changed to ignore the width of the title and
224224
xlabel and the height of the ylabel in the layout logic.
225225

226-
This also means there is a new keyword argument for `.axes.Axes.get_tightbbox`:
227-
``for_layout_only``, which defaults to *False*, but if *True* returns a
228-
bounding box using the rules above. `.axis.Axis.get_tightbbox` gets an
229-
``ignore_label`` keyword argument, which is *None* by default, but which can
230-
also be 'x' or 'y'.
226+
This also means there is a new keyword argument for `.axes.Axes.get_tightbbox`
227+
and `.axis.Axis.get_tightbbox`: ``for_layout_only``, which defaults to *False*,
228+
but if *True* returns a bounding box using the rules above.

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,21 +4143,20 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
41434143
self.apply_aspect()
41444144

41454145
if self.axison:
4146-
igl = 'x' if for_layout_only else None
41474146
try:
4148-
bb_xaxis = self.xaxis.get_tightbbox(renderer, ignore_label=igl)
4147+
bb_xaxis = self.xaxis.get_tightbbox(
4148+
renderer, for_layout_only=for_layout_only)
41494149
except TypeError:
41504150
# in case downstream library has redefined axis:
41514151
bb_xaxis = self.xaxis.get_tightbbox(renderer)
41524152
if bb_xaxis:
41534153
bb.append(bb_xaxis)
4154-
4155-
igl = 'y' if for_layout_only else None
41564154
try:
4157-
bb_yaxis = self.yaxis.get_tightbbox(renderer, ignore_label=igl)
4155+
bb_yaxis = self.yaxis.get_tightbbox(
4156+
renderer, for_layout_only=for_layout_only)
41584157
except TypeError:
41594158
# in case downstream library has redefined axis:
4160-
bb_xaxis = self.yaxis.get_tightbbox(renderer)
4159+
bb_yaxis = self.yaxis.get_tightbbox(renderer)
41614160
if bb_yaxis:
41624161
bb.append(bb_yaxis)
41634162
self._update_title_position(renderer)

lib/matplotlib/axis.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,15 +1079,15 @@ def _get_tick_bboxes(self, ticks, renderer):
10791079
[tick.label2.get_window_extent(renderer)
10801080
for tick in ticks if tick.label2.get_visible()])
10811081

1082-
def get_tightbbox(self, renderer, *, ignore_label=None):
1082+
def get_tightbbox(self, renderer, *, for_layout_only=False):
10831083
"""
10841084
Return a bounding box that encloses the axis. It only accounts
10851085
tick labels, axis label, and offsetText.
10861086
1087-
If ``ignore_label`` is 'x', then the width of the label is collapsed
1088-
to near zero. If 'y', then the height is collapsed to near zero. This
1089-
is for tight/constrained_layout to be able to ignore too-long labels
1090-
when doing their layout.
1087+
If *for_layout_only* is True, then the width of the label (if this
1088+
is an x-axis) or the height of the label (if this is a y-axis) is
1089+
collapsed to near zero. This allows tight/constrained_layout to ignore
1090+
too-long labels when doing their layout.
10911091
"""
10921092
if not self.get_visible():
10931093
return
@@ -1114,14 +1114,15 @@ def get_tightbbox(self, renderer, *, ignore_label=None):
11141114
if self.label.get_visible():
11151115
bb = self.label.get_window_extent(renderer)
11161116
# for constrained/tight_layout, we want to ignore the label's
1117-
# width because the adjustments they make can't be improved.
1117+
# width/height because the adjustments they make can't be improved.
11181118
# this code collapses the relevant direction
1119-
if ignore_label == 'x' and bb.width > 0:
1120-
bb.x0 = (bb.x0 + bb.x1) / 2 - 0.5
1121-
bb.x1 = bb.x0 + 1.0
1122-
elif ignore_label == 'y' and bb.height > 0:
1123-
bb.y0 = (bb.y0 + bb.y1) / 2 - 0.5
1124-
bb.y1 = bb.y0 + 1.0
1119+
if for_layout_only:
1120+
if self.axis_name == "x" and bb.width > 0:
1121+
bb.x0 = (bb.x0 + bb.x1) / 2 - 0.5
1122+
bb.x1 = bb.x0 + 1.0
1123+
if self.axis_name == "y" and bb.height > 0:
1124+
bb.y0 = (bb.y0 + bb.y1) / 2 - 0.5
1125+
bb.y1 = bb.y0 + 1.0
11251126
bboxes.append(bb)
11261127
bboxes = [b for b in bboxes
11271128
if 0 < b.width < np.inf and 0 < b.height < np.inf]

0 commit comments

Comments
 (0)