Skip to content

Commit eaf7dc8

Browse files
committed
FIX: make sure that actual bboxes also work
1 parent 1f60cfb commit eaf7dc8

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,24 +2196,27 @@ 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-
22012199
if bbox_inches is None:
22022200
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+
22032218
if bbox_inches:
22042219
if bbox_inches == "tight":
2205-
renderer = _get_renderer(
2206-
self.figure,
2207-
functools.partial(
2208-
print_method, orientation=orientation)
2209-
)
2210-
ctx = (renderer._draw_disabled()
2211-
if hasattr(renderer, '_draw_disabled')
2212-
else suppress())
2213-
with ctx:
2214-
self.figure.draw(renderer)
2215-
self.figure.set_constrained_layout(False)
2216-
22172220
bbox_inches = self.figure.get_tightbbox(
22182221
renderer, bbox_extra_artists=bbox_extra_artists)
22192222
if pad_inches is None:
@@ -2228,6 +2231,9 @@ def print_figure(
22282231
else:
22292232
_bbox_inches_restore = None
22302233

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)
22312237
try:
22322238
result = print_method(
22332239
filename,
@@ -2244,6 +2250,7 @@ def print_figure(
22442250
self.figure.set_facecolor(origfacecolor)
22452251
self.figure.set_edgecolor(origedgecolor)
22462252
self.figure.set_canvas(self)
2253+
# reset to cached state
22472254
self.figure.set_constrained_layout(cl_state)
22482255
return result
22492256

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 10 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

@@ -511,3 +512,12 @@ def test_manually_set_position():
511512
def test_bboxtight():
512513
fig, ax = plt.subplots(constrained_layout=True)
513514
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)