Skip to content

Backport PR #25499: FIX: use locators in adjust_bbox #25520

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 1 commit into from
Mar 21, 2023
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
5 changes: 4 additions & 1 deletion lib/matplotlib/_tight_bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ def adjust_bbox(fig, bbox_inches, fixed_dpi=None):
locator_list = []
sentinel = object()
for ax in fig.axes:
locator_list.append(ax.get_axes_locator())
locator = ax.get_axes_locator()
if locator is not None:
ax.apply_aspect(locator(ax, None))
locator_list.append(locator)
current_pos = ax.get_position(original=False).frozen()
ax.set_axes_locator(lambda a, r, _pos=current_pos: _pos)
# override the method that enforces the aspect ratio on the Axes
Expand Down
15 changes: 13 additions & 2 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,24 @@ def test_savefig_pixel_ratio(backend):
assert ratio1 == ratio2


def test_savefig_preserve_layout_engine(tmp_path):
def test_savefig_preserve_layout_engine():
fig = plt.figure(layout='compressed')
fig.savefig(tmp_path / 'foo.png', bbox_inches='tight')
fig.savefig(io.BytesIO(), bbox_inches='tight')

assert fig.get_layout_engine()._compress


def test_savefig_locate_colorbar():
fig, ax = plt.subplots()
pc = ax.pcolormesh(np.random.randn(2, 2))
cbar = fig.colorbar(pc, aspect=40)
fig.savefig(io.BytesIO(), bbox_inches=mpl.transforms.Bbox([[0, 0], [4, 4]]))

# Check that an aspect ratio has been applied.
assert (cbar.ax.get_position(original=True).bounds !=
cbar.ax.get_position(original=False).bounds)


def test_figure_repr():
fig = plt.figure(figsize=(10, 20), dpi=10)
assert repr(fig) == "<Figure size 100x200 with 0 Axes>"
Expand Down