diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index acbe52d77a3f..8c11f841cb42 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2198,19 +2198,25 @@ def print_figure( if bbox_inches is None: bbox_inches = rcParams['savefig.bbox'] + + if (self.figure.get_constrained_layout() or + bbox_inches == "tight"): + # we need to trigger a draw before printing to make sure + # CL works. "tight" also needs a draw to get the right + # locations: + renderer = _get_renderer( + self.figure, + functools.partial( + print_method, orientation=orientation) + ) + ctx = (renderer._draw_disabled() + if hasattr(renderer, '_draw_disabled') + else suppress()) + with ctx: + self.figure.draw(renderer) + if bbox_inches: if bbox_inches == "tight": - renderer = _get_renderer( - self.figure, - functools.partial( - print_method, orientation=orientation) - ) - ctx = (renderer._draw_disabled() - if hasattr(renderer, '_draw_disabled') - else suppress()) - with ctx: - self.figure.draw(renderer) - bbox_inches = self.figure.get_tightbbox( renderer, bbox_extra_artists=bbox_extra_artists) if pad_inches is None: @@ -2225,6 +2231,9 @@ def print_figure( else: _bbox_inches_restore = None + # we have already done CL above, so turn it off: + cl_state = self.figure.get_constrained_layout() + self.figure.set_constrained_layout(False) try: result = print_method( filename, @@ -2241,6 +2250,8 @@ def print_figure( self.figure.set_facecolor(origfacecolor) self.figure.set_edgecolor(origedgecolor) self.figure.set_canvas(self) + # reset to cached state + self.figure.set_constrained_layout(cl_state) return result @classmethod diff --git a/lib/matplotlib/tests/baseline_images/test_constrainedlayout/test_bbox.png b/lib/matplotlib/tests/baseline_images/test_constrainedlayout/test_bbox.png new file mode 100644 index 000000000000..f21e247f21fa Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_constrainedlayout/test_bbox.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_constrainedlayout/test_bboxtight.png b/lib/matplotlib/tests/baseline_images/test_constrainedlayout/test_bboxtight.png new file mode 100644 index 000000000000..6e5414ee0a25 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_constrainedlayout/test_bboxtight.png differ diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index 7ea92a160c11..ad7dbcd3bf57 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -4,6 +4,7 @@ from matplotlib.testing.decorators import image_comparison import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec +import matplotlib.transforms as mtransforms from matplotlib import ticker, rcParams @@ -503,3 +504,20 @@ def test_manually_set_position(): fig.canvas.draw() pp = axs[0].get_position() np.testing.assert_allclose(pp, [[0.2, 0.2], [0.44, 0.5]]) + + +@image_comparison(['test_bboxtight.png'], + remove_text=True, style='mpl20', + savefig_kwarg={'bbox_inches': 'tight'}) +def test_bboxtight(): + fig, ax = plt.subplots(constrained_layout=True) + ax.set_aspect(1.) + + +@image_comparison(['test_bbox.png'], + remove_text=True, style='mpl20', + savefig_kwarg={'bbox_inches': + mtransforms.Bbox([[0.5, 0], [2.5, 2]])}) +def test_bbox(): + fig, ax = plt.subplots(constrained_layout=True) + ax.set_aspect(1.) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy