Skip to content

FIX: fix bbox_inches=tight and constrained layout bad interaction #19342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2198,19 +2198,25 @@ def print_figure(

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)

bbox_inches = self.figure.get_tightbbox(
renderer, bbox_extra_artists=bbox_extra_artists)
if pad_inches is None:
Expand All @@ -2225,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,
Expand All @@ -2241,6 +2250,8 @@ 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

@classmethod
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_constrainedlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -503,3 +504,20 @@ 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.)


@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.)