10000 Merge pull request #17287 from anntzer/tbb · matplotlib/matplotlib@fbbc84f · GitHub
[go: up one dir, main page]

Skip to content

Commit fbbc84f

Browse files
authored
Merge pull request #17287 from anntzer/tbb
Make API of get_tightbbox more consistent between Axes and Axis.
2 parents ef8a6ae + d530e47 commit fbbc84f

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

doc/api/api_changes_3.3/behaviour.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,9 @@ x direction, making the axes smaller in the x-direction doesn't help. The
229229
behavior of both has been changed to ignore the width of the title and
230230
xlabel and the height of the ylabel in the layout logic.
231231

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

238236
:rc:`savefig.facecolor` and :rc:`savefig.edgecolor` now default to "auto"
239237
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lib/matplotlib/axes/_base.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,23 +4143,24 @@ 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
4147-
try:
4148-
bb_xaxis = self.xaxis.get_tightbbox(renderer, ignore_label=igl)
4149-
except TypeError:
4150-
# in case downstream library has redefined axis:
4151-
bb_xaxis = self.xaxis.get_tightbbox(renderer)
4152-
if bb_xaxis:
4153-
bb.append(bb_xaxis)
4154-
4155-
igl = 'y' if for_layout_only else None
4156-
try:
4157-
bb_yaxis = self.yaxis.get_tightbbox(renderer, ignore_label=igl)
4158-
except TypeError:
4159-
# in case downstream library has redefined axis:
4160-
bb_xaxis = self.yaxis.get_tightbbox(renderer)
4161-
if bb_yaxis:
4162-
bb.append(bb_yaxis)
4146+
if self.xaxis.get_visible():
4147+
try:
4148+
bb_xaxis = self.xaxis.get_tightbbox(
4149+
renderer, for_layout_only=for_layout_only)
4150+
except TypeError:
4151+
# in case downstream library has redefined axis:
4152+
bb_xaxis = self.xaxis.get_tightbbox(renderer)
4153+
if bb_xaxis:
4154+
bb.append(bb_xaxis)
4155+
if self.yaxis.get_visible():
4156+
try:
4157+
bb_yaxis = self.yaxis.get_tightbbox(
4158+
renderer, for_layout_only=for_layout_only)
4159+
except TypeError:
4160+
# in case downstream library has redefined axis:
4161+
bb_yaxis = self.yaxis.get_tightbbox(renderer)
4162+
if bb_yaxis:
4163+
bb.append(bb_yaxis)
41634164
self._update_title_position(renderer)
41644165
axbbox = self.get_window_extent(renderer)
41654166
bb.append(axbbox)

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)
0