From 1f60cfb4cff83070f25452ff06199dac62cb8bb2 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 22 Jan 2021 14:08:22 -0800 Subject: [PATCH 1/2] FIX: fix bbox_inches=tight and constrained layout bad interaction --- lib/matplotlib/backend_bases.py | 4 ++++ .../test_constrainedlayout/test_bboxtight.png | Bin 0 -> 2686 bytes lib/matplotlib/tests/test_constrainedlayout.py | 8 ++++++++ 3 files changed, 12 insertions(+) create mode 100644 lib/matplotlib/tests/baseline_images/test_constrainedlayout/test_bboxtight.png diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index acbe52d77a3f..9a400c818b7b 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2196,6 +2196,8 @@ def print_figure( self.figure.set_facecolor(facecolor) self.figure.set_edgecolor(edgecolor) + cl_state = self.figure.get_constrained_layout() + if bbox_inches is None: bbox_inches = rcParams['savefig.bbox'] if bbox_inches: @@ -2210,6 +2212,7 @@ def print_figure( else suppress()) with ctx: self.figure.draw(renderer) + self.figure.set_constrained_layout(False) bbox_inches = self.figure.get_tightbbox( renderer, bbox_extra_artists=bbox_extra_artists) @@ -2241,6 +2244,7 @@ def print_figure( self.figure.set_facecolor(origfacecolor) self.figure.set_edgecolor(origedgecolor) self.figure.set_canvas(self) + self.figure.set_constrained_layout(cl_state) return result @classmethod 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 0000000000000000000000000000000000000000..6e5414ee0a2539526dc1d4ff830eae05046daf8d GIT binary patch literal 2686 zcmeAS@N?(olHy`uVBq!ia0y~yV0;Y19Be=lf%0+-AQfEV8c`CQpH@;1Sh=j`e^HvL;#!1Jsry@X=gys&R11cRRo`mm@9(RX&cC-uQa|p`zc2a1cT ztF7xo0Blp?ne*Lu6ozG$zKIGha^Lw9sWn3H1k9+z0f<~lG` zbX_Zz{cI!u>dC|FS1z0MX3mnckzx3tEWU#QSB4*rAW|a;R7`?G?akARt+%)J=H7lc z|9a`(Qw|It?#S3l&OiU$@cie$>5y_x7=Ex!=#f-W8Xs zz)+FLH^1ln^UpJ$|NOUE=d|f_riS##Y4^YXJ#i?0ZqM{oNnnLF$8Y&+UJeHTi@Vk( z&wK7X*XGZxs#(hneJ?k+Gc(jZVG>8FGDjnbk_dveL-yM2D!9FEWA5$0pPv>`=+{CS`q@9*uEj^AG=yFPC3 zueY}jt3S@1b|f4^EuGal5!w$anD{;27zy*l?uMUAzopr04so@XaE2J literal 0 HcmV?d00001 diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index 7ea92a160c11..a36144f5bc8c 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -503,3 +503,11 @@ 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.) From eaf7dc8c4ed921f12b6f04754a22c0a23995b80a Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Sat, 23 Jan 2021 08:12:01 -0800 Subject: [PATCH 2/2] FIX: make sure that actual bboxes also work --- lib/matplotlib/backend_bases.py | 35 +++++++++++------- .../test_constrainedlayout/test_bbox.png | Bin 0 -> 978 bytes .../tests/test_constrainedlayout.py | 10 +++++ 3 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 lib/matplotlib/tests/baseline_images/test_constrainedlayout/test_bbox.png diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 9a400c818b7b..8c11f841cb42 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2196,24 +2196,27 @@ def print_figure( self.figure.set_facecolor(facecolor) self.figure.set_edgecolor(edgecolor) - cl_state = self.figure.get_constrained_layout() - 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) - self.figure.set_constrained_layout(False) - bbox_inches = self.figure.get_tightbbox( renderer, bbox_extra_artists=bbox_extra_artists) if pad_inches is None: @@ -2228,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, @@ -2244,6 +2250,7 @@ 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 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 0000000000000000000000000000000000000000..f21e247f21fae07c6a82bb95735858265b307224 GIT binary patch literal 978 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k4M?tyST_$y1(&!+lmzFem6RtIr84*?mK5aV zm*iw7DU_ua6=&w>8S5GA8R-?|7nc|r85roLq?Q?IryHi386>AAnIxy_C}fnB6ck(O z>*vC?>g5-u>mTXeo(MFav%n*=n1O*G$mUOETw%XFlYxO*)zif>q+-t7n}%6WJR}+} zeo6{Zo9pmnh3q%mx7<6XYF<+5jD06(rg!k$vo;64UoCP>qPO}^NUfyvx&pvMVkIgo(@5jDV lm0kDblRC#7PX&QG#!RXCPyW1XY6s>b22WQ%mvv4FO#o;kBq#s? literal 0 HcmV?d00001 diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index a36144f5bc8c..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 @@ -511,3 +512,12 @@ def test_manually_set_position(): 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