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

Skip to content

Commit c881d0c

Browse files
authored
Merge pull request #21021 from anntzer/tbflo
Factor out for_layout_only backcompat support in get_tightlayout.
2 parents b9ea8ce + 556651f commit c881d0c

File tree

5 files changed

+24
-34
lines changed

5 files changed

+24
-34
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import numpy as np
1919

20-
from matplotlib import _api
20+
from matplotlib import _api, artist as martist
2121
import matplotlib.transforms as mtransforms
2222
import matplotlib._layoutgrid as mlayoutgrid
2323

@@ -534,17 +534,12 @@ def get_pos_and_bbox(ax, renderer):
534534
Position in figure coordinates.
535535
bbox : Bbox
536536
Tight bounding box in figure coordinates.
537-
538537
"""
539538
fig = ax.figure
540539
pos = ax.get_position(original=True)
541540
# pos is in panel co-ords, but we need in figure for the layout
542541
pos = pos.transformed(fig.transSubfigure - fig.transFigure)
543-
try:
544-
tightbbox = ax.get_tightbbox(renderer=renderer, for_layout_only=True)
545-
except TypeError:
546-
tightbbox = ax.get_tightbbox(renderer=renderer)
547-
542+
tightbbox = martist._get_tightbbox_for_layout_only(ax, renderer)
548543
if tightbbox is None:
549544
bbox = pos
550545
else:

lib/matplotlib/artist.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,18 @@ def mouseover(self, val):
13041304
ax._mouseover_set.discard(self)
13051305

13061306

1307+
def _get_tightbbox_for_layout_only(obj, *args, **kwargs):
1308+
"""
1309+
Matplotlib's `.Axes.get_tightbbox` and `.Axis.get_tightbbox` support a
1310+
*for_layout_only* kwarg; this helper tries to uses the kwarg but skips it
1311+
when encountering third-party subclasses that do not support it.
1312+
"""
1313+
try:
1314+
return obj.get_tightbbox(*args, **{**kwargs, "for_layout_only": True})
1315+
except TypeError:
1316+
return obj.get_tightbbox(*args, **kwargs)
1317+
1318+
13071319
class ArtistInspector:
13081320
"""
13091321
A helper class to inspect an `~matplotlib.artist.Artist` and return

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4582,21 +4582,13 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
45824582

45834583
if self.axison:
45844584
if self.xaxis.get_visible():
4585-
try:
4586-
bb_xaxis = self.xaxis.get_tightbbox(
4587-
renderer, for_layout_only=for_layout_only)
4588-
except TypeError:
4589-
# in case downstream library has redefined axis:
4590-
bb_xaxis = self.xaxis.get_tightbbox(renderer)
4585+
bb_xaxis = martist._get_tightbbox_for_layout_only(
4586+
self.xaxis, renderer)
45914587
if bb_xaxis:
45924588
bb.append(bb_xaxis)
45934589
if self.yaxis.get_visible():
4594-
try:
4595-
bb_yaxis = self.yaxis.get_tightbbox(
4596-
renderer, for_layout_only=for_layout_only)
4597-
except TypeError:
4598-
# in case downstream library has redefined axis:
4599-
bb_yaxis = self.yaxis.get_tightbbox(renderer)
4590+
bb_yaxis = martist._get_tightbbox_for_layout_only(
4591+
self.yaxis, renderer)
46004592
if bb_yaxis:
46014593
bb.append(bb_yaxis)
46024594
self._update_title_position(renderer)

lib/matplotlib/tight_layout.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import numpy as np
1313

14-
from matplotlib import _api, rcParams
14+
from matplotlib import _api, artist as martist, rcParams
1515
from matplotlib.font_manager import FontProperties
1616
from matplotlib.transforms import Bbox
1717

@@ -78,10 +78,7 @@ def _auto_adjust_subplotpars(
7878
bb = []
7979
for ax in subplots:
8080
if ax.get_visible():
81-
try:
82-
bb += [ax.get_tightbbox(renderer, for_layout_only=True)]
83-
except TypeError:
84-
bb += [ax.get_tightbbox(renderer)]
81+
bb += [martist._get_tightbbox_for_layout_only(ax, renderer)]
8582

8683
tight_bbox_raw = Bbox.union(bb)
8784
tight_bbox = fig.transFigure.inverted().transform_bbox(tight_bbox_raw)

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3287,16 +3287,10 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
32873287
if self._axis3don:
32883288
for axis in self._get_axis_list():
32893289
if axis.get_visible():
3290-
try:
3291-
axis_bb = axis.get_tightbbox(
3292-
renderer,
3293-
for_layout_only=for_layout_only
3294-
)
3295-
except TypeError:
3296-
# in case downstream library has redefined axis:
3297-
axis_bb = axis.get_tightbbox(renderer)
3298-
if axis_bb:
3299-
batch.append(axis_bb)
3290+
axis_bb = martist._get_tightbbox_for_layout_only(
3291+
axis, renderer)
3292+
if axis_bb:
3293+
batch.append(axis_bb)
33003294
return mtransforms.Bbox.union(batch)
33013295

33023296
def stem(self, x, y, z, *, linefmt='C0-', markerfmt='C0o', basefmt='C3-',

0 commit comments

Comments
 (0)