diff --git a/lib/matplotlib/_tight_bbox.py b/lib/matplotlib/_tight_bbox.py index e0fba389d120..db72bbdff020 100644 --- a/lib/matplotlib/_tight_bbox.py +++ b/lib/matplotlib/_tight_bbox.py @@ -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 diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index f51ce72937a2..cd786f4418f7 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -546,13 +546,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) + + @mpl.rc_context({"savefig.transparent": True}) @check_figures_equal(extensions=["png"]) def test_savefig_transparent(fig_test, fig_ref):
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: