Skip to content

Commit 1f60cfb

Browse files
committed
FIX: fix bbox_inches=tight and constrained layout bad interaction
1 parent 408b1ab commit 1f60cfb

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,8 @@ def print_figure(
21962196
self.figure.set_facecolor(facecolor)
21972197
self.figure.set_edgecolor(edgecolor)
21982198

2199+
cl_state = self.figure.get_constrained_layout()
2200+
21992201
if bbox_inches is None:
22002202
bbox_inches = rcParams['savefig.bbox']
22012203
if bbox_inches:
@@ -2210,6 +2212,7 @@ def print_figure(
22102212
else suppress())
22112213
with ctx:
22122214
self.figure.draw(renderer)
2215+
self.figure.set_constrained_layout(False)
22132216

22142217
bbox_inches = self.figure.get_tightbbox(
22152218
renderer, bbox_extra_artists=bbox_extra_artists)
@@ -2241,6 +2244,7 @@ def print_figure(
22412244
self.figure.set_facecolor(origfacecolor)
22422245
self.figure.set_edgecolor(origedgecolor)
22432246
self.figure.set_canvas(self)
2247+
self.figure.set_constrained_layout(cl_state)
22442248
return result
22452249

22462250
@classmethod

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,11 @@ def test_manually_set_position():
503503
fig.canvas.draw()
504504
pp = axs[0].get_position()
505505
np.testing.assert_allclose(pp, [[0.2, 0.2], [0.44, 0.5]])
506+
507+
508+
@image_comparison(['test_bboxtight.png'],
509+
remove_text=True, style='mpl20',
510+
savefig_kwarg={'bbox_inches': 'tight'})
511+
def test_bboxtight():
512+
fig, ax = plt.subplots(constrained_layout=True)
513+
ax.set_aspect(1.)

0 commit comments

Comments
 (0)