Skip to content

Commit e0de606

Browse files
authored
Merge pull request #25520 from rcomer/backport-25499
Backport PR #25499: FIX: use locators in adjust_bbox
2 parents c37415e + a42649e commit e0de606

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/matplotlib/_tight_bbox.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ def adjust_bbox(fig, bbox_inches, fixed_dpi=None):
2323
locator_list = []
2424
sentinel = object()
2525
for ax in fig.axes:
26-
locator_list.append(ax.get_axes_locator())
26+
locator = ax.get_axes_locator()
27+
if locator is not None:
28+
ax.apply_aspect(locator(ax, None))
29+
locator_list.append(locator)
2730
current_pos = ax.get_position(original=False).frozen()
2831
ax.set_axes_locator(lambda a, r, _pos=current_pos: _pos)
2932
# override the method that enforces the aspect ratio on the Axes

lib/matplotlib/tests/test_figure.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,13 +532,24 @@ def test_savefig_pixel_ratio(backend):
532532
assert ratio1 == ratio2
533533

534534

535-
def test_savefig_preserve_layout_engine(tmp_path):
535+
def test_savefig_preserve_layout_engine():
536536
fig = plt.figure(layout='compressed')
537-
fig.savefig(tmp_path / 'foo.png', bbox_inches='tight')
537+
fig.savefig(io.BytesIO(), bbox_inches='tight')
538538

539539
assert fig.get_layout_engine()._compress
540540

541541

542+
def test_savefig_locate_colorbar():
543+
fig, ax = plt.subplots()
544+
pc = ax.pcolormesh(np.random.randn(2, 2))
545+
cbar = fig.colorbar(pc, aspect=40)
546+
fig.savefig(io.BytesIO(), bbox_inches=mpl.transforms.Bbox([[0, 0], [4, 4]]))
547+
548+
# Check that an aspect ratio has been applied.
549+
assert (cbar.ax.get_position(original=True).bounds !=
550+
cbar.ax.get_position(original=False).bounds)
551+
552+
542553
def test_figure_repr():
543554
fig = plt.figure(figsize=(10, 20), dpi=10)
544555
assert repr(fig) == "<Figure size 100x200 with 0 Axes>"

0 commit comments

Comments
 (0)