Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Make API of get_tightbbox more consistent between Axes and Axis. #17287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions doc/api/api_changes_3.3/behaviour.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,9 @@ x direction, making the axes smaller in the x-direction doesn't help. The
behavior of both has been changed to ignore the width of the title and
xlabel and the height of the ylabel in the layout logic.

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

:rc:`savefig.facecolor` and :rc:`savefig.edgecolor` now default to "auto"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
35 changes: 18 additions & 17 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4143,23 +4143,24 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
self.apply_aspect()

if self.axison:
igl = 'x' if for_layout_only else None
try:
bb_xaxis = self.xaxis.get_tightbbox(renderer, ignore_label=igl)
except TypeError:
# in case downstream library has redefined axis:
bb_xaxis = self.xaxis.get_tightbbox(renderer)
if bb_xaxis:
bb.append(bb_xaxis)

igl = 'y' if for_layout_only else None
try:
bb_yaxis = self.yaxis.get_tightbbox(renderer, ignore_label=igl)
except TypeError:
# in case downstream library has redefined axis:
bb_xaxis = self.yaxis.get_tightbbox(renderer)
if bb_yaxis:
bb.append(bb_yaxis)
if self.xaxis.get_visible():
try:
bb_xaxis = self.xaxis.get_tightbbox(
renderer, for_layout_only=for_layout_only)
except TypeError:
# in case downstream library has redefined axis:
bb_xaxis = self.xaxis.get_tightbbox(renderer)
if bb_xaxis:
bb.append(bb_xaxis)
if self.yaxis.get_visible():
try:
bb_yaxis = self.yaxis.get_tightbbox(
renderer, for_layout_only=for_layout_only)
except TypeError:
# in case downstream library has redefined axis:
bb_yaxis = self.yaxis.get_tightbbox(renderer)
if bb_yaxis:
bb.append(bb_yaxis)
self._update_title_position(renderer)
axbbox = self.get_window_extent(renderer)
bb.append(axbbox)
Expand Down
25 changes: 13 additions & 12 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,15 +1079,15 @@ def _get_tick_bboxes(self, ticks, renderer):
[tick.label2.get_window_extent(renderer)
for tick in ticks if tick.label2.get_visible()])

def get_tightbbox(self, renderer, *, ignore_label=None):
def get_tightbbox(self, renderer, *, for_layout_only=False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm 50/50 on this change. Consistency is nice, OTOH, the kwarg name doesn't make a lot of sense at this level. But since the docs refer to *_layout maybe its OK.

"""
Return a bounding box that encloses the axis. It only accounts
tick labels, axis label, and offsetText.

If ``ignore_label`` is 'x', then the width of the label is collapsed
to near zero. If 'y', then the height is collapsed to near zero. This
is for tight/constrained_layout to be able to ignore too-long labels
when doing their layout.
If *for_layout_only* is True, then the width of the label (if this
is an x-axis) or the height of the label (if this is a y-axis) is
collapsed to near zero. This allows tight/constrained_layout to ignore
too-long labels when doing their layout.
"""
if not self.get_visible():
return
Expand All @@ -1114,14 +1114,15 @@ def get_tightbbox(self, renderer, *, ignore_label=None):
if self.label.get_visible():
bb = self.label.get_window_extent(renderer)
# for constrained/tight_layout, we want to ignore the label's
# width because the adjustments they make can't be improved.
# width/height because the adjustments they make can't be improved.
# this code collapses the relevant direction
if ignore_label == 'x' and bb.width > 0:
bb.x0 = (bb.x0 + bb.x1) / 2 - 0.5
bb.x1 = bb.x0 + 1.0
elif ignore_label == 'y' and bb.height > 0:
bb.y0 = (bb.y0 + bb.y1) / 2 - 0.5
bb.y1 = bb.y0 + 1.0
if for_layout_only:
if self.axis_name == "x" and bb.width > 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and I guess I still don't particularly like the introspection. But, again if other folks think the consistency is more important than explicitness I won't block.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a bit of paranoia here with axis_name = getattr(self, 'axis_name', '')?

bb.x0 = (bb.x0 + bb.x1) / 2 - 0.5
bb.x1 = bb.x0 + 1.0
if self.axis_name == "y" and bb.height > 0:
bb.y0 = (bb.y0 + bb.y1) / 2 - 0.5
bb.y1 = bb.y0 + 1.0
bboxes.append(bb)
bboxes = [b for b in bboxes
if 0 < b.width < np.inf and 0 < b.height < np.inf]
Expand Down