Skip to content
Open
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
5 changes: 4 additions & 1 deletion lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4747,7 +4747,10 @@ def get_tightbbox(self, renderer=None, *, call_axes_locator=True,

for axis in self._axis_map.values():
if self.axison and axis.get_visible():
ba = martist._get_tightbbox_for_layout_only(axis, renderer)
if for_layout_only:
ba = martist._get_tightbbox_for_layout_only(axis, renderer)
else:
ba = axis.get_tightbbox(renderer)
if ba:
bb.append(ba)
self._update_title_position(renderer)
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8374,6 +8374,21 @@ def test_tick_padding_tightbbox():
assert bb.y0 < bb2.y0


def test_tightbbox_includes_long_label():
fig, ax = plt.subplots()

renderer = fig._get_renderer()
bbox_no_xlabel = ax.get_tightbbox(renderer, for_layout_only=False)

ax.set_xlabel(
'loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong')
bbox_long_xlabel = ax.get_tightbbox(renderer, for_layout_only=False)

# When for_layout_only is False, the axes tightbbox should encompass its labels even
# if they are long enough to extent beyond its limits.
assert bbox_long_xlabel.width > bbox_no_xlabel.width


def test_inset():
"""
Ensure that inset_ax argument is indeed optional
Expand Down
6 changes: 4 additions & 2 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4167,8 +4167,10 @@ def get_tightbbox(self, renderer=None, *, call_axes_locator=True,
if self._axis3don:
for axis in self._axis_map.values():
if axis.get_visible():
axis_bb = martist._get_tightbbox_for_layout_only(
axis, renderer)
if for_layout_only:
axis_bb = martist._get_tightbbox_for_layout_only(axis, renderer)
else:
axis_bb = axis.get_tightbbox(renderer=renderer)
if axis_bb:
batch.append(axis_bb)
return mtransforms.Bbox.union(batch)
Expand Down
21 changes: 21 additions & 0 deletions lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -3202,3 +3202,24 @@ def test_plot_surface_log_scale_invalid_values():

zmin, zmax = ax.get_zlim()
assert 1e-3 < zmin < zmax < 1e3, f"zlim corrupted: {(zmin, zmax)}"


def test_axes3d_tightbbox_includes_axis_labels():
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter([1], [1], [1])

fig.draw_without_rendering()
renderer = fig._get_renderer()
bb_no_labels = ax.get_tightbbox(renderer)

ax.set_xlabel('X label')
ax.set_ylabel('Y label')
ax.set_zlabel('Z label')

fig.draw_without_rendering()
bb_full = ax.get_tightbbox(renderer)

# The full bbox must be strictly larger in at least one dimension than the
# bbox not including labels.
assert bb_full.width > bb_no_labels.width or bb_full.height > bb_no_labels.height
Loading