Skip to content

Commit b6294ff

Browse files
authored
Merge pull request #19342 from jklymak/fix-CL-tight
FIX: fix bbox_inches=tight and constrained layout bad interaction
2 parents af1f467 + eaf7dc8 commit b6294ff

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,19 +2198,25 @@ def print_figure(
21982198

21992199
if bbox_inches is None:
22002200
bbox_inches = rcParams['savefig.bbox']
2201+
2202+
if (self.figure.get_constrained_layout() or
2203+
bbox_inches == "tight"):
2204+
# we need to trigger a draw before printing to make sure
2205+
# CL works. "tight" also needs a draw to get the right
2206+
# locations:
2207+
renderer = _get_renderer(
2208+
self.figure,
2209+
functools.partial(
2210+
print_method, orientation=orientation)
2211+
)
2212+
ctx = (renderer._draw_disabled()
2213+
if hasattr(renderer, '_draw_disabled')
2214+
else suppress())
2215+
with ctx:
2216+
self.figure.draw(renderer)
2217+
22012218
if bbox_inches:
22022219
if bbox_inches == "tight":
2203-
renderer = _get_renderer(
2204-
self.figure,
2205-
functools.partial(
2206-
print_method, orientation=orientation)
2207-
)
2208-
ctx = (renderer._draw_disabled()
2209-
if hasattr(renderer, '_draw_disabled')
2210-
else suppress())
2211-
with ctx:
2212-
self.figure.draw(renderer)
2213-
22142220
bbox_inches = self.figure.get_tightbbox(
22152221
renderer, bbox_extra_artists=bbox_extra_artists)
22162222
if pad_inches is None:
@@ -2225,6 +2231,9 @@ def print_figure(
22252231
else:
22262232
_bbox_inches_restore = None
22272233

2234+
# we have already done CL above, so turn it off:
2235+
cl_state = self.figure.get_constrained_layout()
2236+
self.figure.set_constrained_layout(False)
22282237
try:
22292238
result = print_method(
22302239
filename,
@@ -2241,6 +2250,8 @@ def print_figure(
22412250
self.figure.set_facecolor(origfacecolor)
22422251
self.figure.set_edgecolor(origedgecolor)
22432252
self.figure.set_canvas(self)
2253+
# reset to cached state
2254+
self.figure.set_constrained_layout(cl_state)
22442255
return result
22452256

22462257
@classmethod

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from matplotlib.testing.decorators import image_comparison
55
import matplotlib.pyplot as plt
66
import matplotlib.gridspec as gridspec
7+
import matplotlib.transforms as mtransforms
78
from matplotlib import ticker, rcParams
89

910

@@ -503,3 +504,20 @@ def test_manually_set_position():
503504
fig.canvas.draw()
504505
pp = axs[0].get_position()
505506
np.testing.assert_allclose(pp, [[0.2, 0.2], [0.44, 0.5]])
507+
508+
509+
@image_comparison(['test_bboxtight.png'],
510+
remove_text=True, style='mpl20',
511+
savefig_kwarg={'bbox_inches': 'tight'})
512+
def test_bboxtight():
513+
fig, ax = plt.subplots(constrained_layout=True)
514+
ax.set_aspect(1.)
515+
516+
517+
@image_comparison(['test_bbox.png'],
518+
remove_text=True, style='mpl20',
519+
savefig_kwarg={'bbox_inches':
520+
mtransforms.Bbox([[0.5, 0], [2.5, 2]])})
521+
def test_bbox():
522+
fig, ax = plt.subplots(constrained_layout=True)
523+
ax.set_aspect(1.)

0 commit comments

Comments
 (0)