Skip to content

Commit 50aa98a

Browse files
committed
FIX: use locators in adjust_bbox
1 parent 78bf53c commit 50aa98a

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
@@ -546,13 +546,24 @@ def test_savefig_pixel_ratio(backend):
546546
assert ratio1 == ratio2
547547

548548

549-
def test_savefig_preserve_layout_engine(tmp_path):
549+
def test_savefig_preserve_layout_engine():
550550
fig = plt.figure(layout='compressed')
551-
fig.savefig(tmp_path / 'foo.png', bbox_inches='tight')
551+
fig.savefig(io.BytesIO(), bbox_inches='tight')
552552

553553
assert fig.get_layout_engine()._compress
554554

555555

556+
def test_savefig_locate_colorbar():
557+
fig, ax = plt.subplots()
558+
pc = ax.pcolormesh(np.random.randn(2, 2))
559+
cbar = fig.colorbar(pc, aspect=40)
560+
fig.savefig(io.BytesIO(), bbox_inches=mpl.transforms.Bbox([[0, 0], [4, 4]]))
561+
562+
# Check that an aspect ratio has been applied.
563+
assert (cbar.ax.get_position(original=True).bounds !=
564+
cbar.ax.get_position(original=False).bounds)
565+
566+
556567
@mpl.rc_context({"savefig.transparent": True})
557568
@check_figures_equal(extensions=["png"])
558569
def test_savefig_transparent(fig_test, fig_ref):

0 commit comments

Comments
 (0)