Skip to content

Commit e1ed421

Browse files
authored
Merge pull request matplotlib#31972 from meeseeksmachine/auto-backport-of-pr-31932-on-v3.11.x
Backport PR matplotlib#31932 on branch v3.11.x (FIX: include axis labels in get_tightbbox when not for_layout_only)
2 parents c6ebc9b + 578ebdf commit e1ed421

4 files changed

Lines changed: 44 additions & 3 deletions

File tree

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4742,7 +4742,10 @@ def get_tightbbox(self, renderer=None, *, call_axes_locator=True,
47424742

47434743
for axis in self._axis_map.values():
47444744
if self.axison and axis.get_visible():
4745-
ba = martist._get_tightbbox_for_layout_only(axis, renderer)
4745+
if for_layout_only:
4746+
ba = martist._get_tightbbox_for_layout_only(axis, renderer)
4747+
else:
4748+
ba = axis.get_tightbbox(renderer)
47464749
if ba:
47474750
bb.append(ba)
47484751
self._update_title_position(renderer)

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8330,6 +8330,21 @@ def test_tick_padding_tightbbox():
83308330
assert bb.y0 < bb2.y0
83318331

83328332

8333+
def test_tightbbox_includes_long_label():
8334+
fig, ax = plt.subplots()
8335+
8336+
renderer = fig._get_renderer()
8337+
bbox_no_xlabel = ax.get_tightbbox(renderer, for_layout_only=False)
8338+
8339+
ax.set_xlabel(
8340+
'loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong')
8341+
bbox_long_xlabel = ax.get_tightbbox(renderer, for_layout_only=False)
8342+
8343+
# When for_layout_only is False, the axes tightbbox should encompass its labels even
8344+
# if they are long enough to extent beyond its limits.
8345+
assert bbox_long_xlabel.width > bbox_no_xlabel.width
8346+
8347+
83338348
def test_inset():
83348349
"""
83358350
Ensure that inset_ax argument is indeed optional

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4167,8 +4167,10 @@ def get_tightbbox(self, renderer=None, *, call_axes_locator=True,
41674167
if self._axis3don:
41684168
for axis in self._axis_map.values():
41694169
if axis.get_visible():
4170-
axis_bb = martist._get_tightbbox_for_layout_only(
4171-
axis, renderer)
4170+
if for_layout_only:
4171+
axis_bb = martist._get_tightbbox_for_layout_only(axis, renderer)
4172+
else:
4173+
axis_bb = axis.get_tightbbox(renderer=renderer)
41724174
if axis_bb:
41734175
batch.append(axis_bb)
41744176
return mtransforms.Bbox.union(batch)

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,3 +3204,24 @@ def test_plot_surface_log_scale_invalid_values():
32043204

32053205
zmin, zmax = ax.get_zlim()
32063206
assert 1e-3 < zmin < zmax < 1e3, f"zlim corrupted: {(zmin, zmax)}"
3207+
3208+
3209+
def test_axes3d_tightbbox_includes_axis_labels():
3210+
fig = plt.figure()
3211+
ax = fig.add_subplot(projection='3d')
3212+
ax.scatter([1], [1], [1])
3213+
3214+
fig.draw_without_rendering()
3215+
renderer = fig._get_renderer()
3216+
bb_no_labels = ax.get_tightbbox(renderer)
3217+
3218+
ax.set_xlabel('X label')
3219+
ax.set_ylabel('Y label')
3220+
ax.set_zlabel('Z label')
3221+
3222+
fig.draw_without_rendering()
3223+
bb_full = ax.get_tightbbox(renderer)
3224+
3225+
# The full bbox must be strictly larger in at least one dimension than the
3226+
# bbox not including labels.
3227+
assert bb_full.width > bb_no_labels.width or bb_full.height > bb_no_labels.height

0 commit comments

Comments
 (0)