Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d973d9c

Browse filesBrowse files
committed
FIX: make sure that actual bboxes also work
1 parent 1f60cfb commit d973d9c
Copy full SHA for d973d9c

File tree

Expand file treeCollapse file tree

3 files changed

+32
-14
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+32
-14
lines changed

‎lib/matplotlib/backend_bases.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backend_bases.py
+21-14Lines 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:
@@ -2229,6 +2232,9 @@ def print_figure(
22292232
_bbox_inches_restore = None
22302233

22312234
try:
2235+
# we have already done CL above, so turn it off:
2236+
cl_state = self.figure.get_constrained_layout()
2237+
self.figure.set_constrained_layout(False)
22322238
result = print_method(
22332239
filename,
22342240
dpi=dpi,
@@ -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

Loading

‎lib/matplotlib/tests/test_constrainedlayout.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_constrainedlayout.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
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

11+
1012
def example_plot(ax, fontsize=12, nodec=False):
1113
ax.plot([1, 2])
1214
ax.locator_params(nbins=3)
@@ -511,3 +513,12 @@ def test_manually_set_position():
511513
def test_bboxtight():
512514
fig, ax = plt.subplots(constrained_layout=True)
513515
ax.set_aspect(1.)
516+
517+
518+
@image_comparison(['test_bbox.png'],
519+
remove_text=True, style='mpl20',
520+
savefig_kwarg={'bbox_inches':
521+
mtransforms.Bbox([[0.5, 0], [2.5, 2]])})
522+
def test_bbox():
523+
fig, ax = plt.subplots(constrained_layout=True)
524+
ax.set_aspect(1.)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.