Skip to content

Emit "axes not compatible with tight_layout" in a single place. #24343

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 1 commit into from
Nov 3, 2022
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
5 changes: 4 additions & 1 deletion lib/matplotlib/_tight_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
ss_to_subplots = {ss: [] for ss in subplotspec_list}
for ax, ss in zip(axes_list, subplotspec_list):
ss_to_subplots[ss].append(ax)
ss_to_subplots.pop(None, None) # Skip subplotspec == None.
if ss_to_subplots.pop(None, None):
_api.warn_external(
"This figure includes Axes that are not compatible with "
"tight_layout, so results might be incorrect.")
if not ss_to_subplots:
return {}
subplot_list = list(ss_to_subplots.values())
Expand Down
6 changes: 0 additions & 6 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3424,12 +3424,6 @@ def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None):
.Figure.set_layout_engine
.pyplot.tight_layout
"""
from ._tight_layout import get_subplotspec_list
subplotspec_list = get_subplotspec_list(self.axes)
if None in subplotspec_list:
_api.warn_external("This figure includes Axes that are not "
"compatible with tight_layout, so results "
"might be incorrect.")
# note that here we do not permanently set the figures engine to
# tight_layout but rather just perform the layout in place and remove
# any previous engines.
Expand Down
14 changes: 3 additions & 11 deletions lib/matplotlib/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,20 +467,12 @@ def tight_layout(self, figure, renderer=None,
coordinates that the whole subplots area (including labels) will
fit into. Default (None) is the whole figure.
"""

subplotspec_list = _tight_layout.get_subplotspec_list(
figure.axes, grid_spec=self)
if None in subplotspec_list:
_api.warn_external("This figure includes Axes that are not "
"compatible with tight_layout, so results "
"might be incorrect.")

if renderer is None:
renderer = figure._get_renderer()

kwargs = _tight_layout.get_tight_layout_figure(
figure, figure.axes, subplotspec_list, renderer,
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
figure, figure.axes,
_tight_layout.get_subplotspec_list(figure.axes, grid_spec=self),
renderer, pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
if kwargs:
self.update(**kwargs)

Expand Down
8 changes: 1 addition & 7 deletions lib/matplotlib/layout_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from contextlib import nullcontext

import matplotlib as mpl
import matplotlib._api as _api

from matplotlib._constrained_layout import do_constrained_layout
from matplotlib._tight_layout import (get_subplotspec_list,
Expand Down Expand Up @@ -170,15 +169,10 @@ def execute(self, fig):
See also: `.figure.Figure.tight_layout` and `.pyplot.tight_layout`.
"""
info = self._params
subplotspec_list = get_subplotspec_list(fig.axes)
if None in subplotspec_list:
_api.warn_external("This figure includes Axes that are not "
"compatible with tight_layout, so results "
"might be incorrect.")
renderer = fig._get_renderer()
with getattr(renderer, "_draw_disabled", nullcontext)():
kwargs = get_tight_layout_figure(
fig, fig.axes, subplotspec_list, renderer,
fig, fig.axes, get_subplotspec_list(fig.axes), renderer,
pad=info['pad'], h_pad=info['h_pad'], w_pad=info['w_pad'],
rect=info['rect'])
if kwargs:
Expand Down