8000 implement tight_layout.get_subplotspec_list and uses this to check if… · matplotlib/matplotlib@77e245a · GitHub
[go: up one dir, main page]

Skip to content

Commit 77e245a

Browse files
committed
implement tight_layout.get_subplotspec_list and uses this to check if all the axes are supported by tight_layout
1 parent 0c7cdaa commit 77e245a

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

lib/matplotlib/figure.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,17 +1422,17 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=Non
14221422
labels) will fit into. Default is (0, 0, 1, 1).
14231423
"""
14241424

1425-
from tight_layout import get_renderer, get_tight_layout_figure
1425+
from tight_layout import get_renderer, get_tight_layout_figure, \
1426+
get_subplotspec_list
14261427

1427-
subplot_axes = [ax for ax in self.axes if isinstance(ax, SubplotBase)]
1428-
if len(subplot_axes) < len(self.axes):
1428+
if None in get_subplotspec_list(self.axes):
14291429
warnings.warn("tight_layout can only process Axes that descend "
14301430
"from SubplotBase; results might be incorrect.")
14311431

14321432
if renderer is None:
14331433
renderer = get_renderer(self)
14341434

1435-
kwargs = get_tight_layout_figure(self, subplot_axes, renderer,
1435+
kwargs = get_tight_layout_figure(self, self.axes, renderer,
14361436
pad=pad, h_pad=h_pad, w_pad=w_pad,
14371437
rect=rect)
14381438

lib/matplotlib/tight_layout.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,33 @@ def get_renderer(fig):
209209
return renderer
210210

211211

212+
def get_subplotspec_list(axes_list):
213+
"""
214+
Return a list of subplotspec from the given list of axes. For an
215+
instance of axes that does not support subplotspec, None is
216+
inserted in the list.
217+
218+
"""
219+
subplotspec_list = []
220+
for ax in axes_list:
221+
locator = ax.get_axes_locator()
222+
if hasattr(locator, "get_subplotspec"):
223+
subplotspec = locator.get_subplotspec().get_topmost_subplotspec()
224+
elif hasattr(ax, "get_subplotspec"):
225+
subplotspec = ax.get_subplotspec().get_topmost_subplotspec()
226+
else:
227+
subplotspec = None
228+
229+
if subplotspec is not None and \
230+
subplotspec.get_gridspec().locally_modified_subplot_params():
231+
232+
subplotspec = None
233+
234+
subplotspec_list.append(subplotspec)
235+
236+
return subplotspec_list
237+
238+
212239
def get_tight_layout_figure(fig, axes_list, renderer,
213240
pad=1.08, h_pad=None, w_pad=None, rect=None):
214241
"""
@@ -244,21 +271,13 @@ def get_tight_layout_figure(fig, axes_list, renderer,
244271
ncols_list = []
245272
ax_bbox_list = []
246273

247-
subplot_dict = {} # for axes_grid1, multiple axes can share
248-
# same subplot_interface. Thus we need to
249-
# join them together.
250-
251-
for ax in axes_list:
252-
locator = ax.get_axes_locator()
253-
if hasattr(locator, "get_subplotspec"):
254-
subplotspec = locator.get_subplotspec().get_topmost_subplotspec()
255-
elif hasattr(ax, "get_subplotspec"):
256-
subplotspec = ax.get_subplotspec().get_topmost_subplotspec()
257-
else:
258-
continue
274+
subplot_dict = {} # multiple axes can share
275+
# same subplot_interface (e.g, axes_grid1). Thus
276+
# we need to join them together.
259277

260-
if (subplotspec is None) or \
261-
subplotspec.get_gridspec().locally_modified_subplot_params():
278+
for ax, subplotspec in zip(axes_list,
279+
get_subplotspec_list(axes_list)):
280+
if subplotspec is None:
262281
continue
263282

264283
subplots = subplot_dict.setdefault(subplotspec, [])

0 commit comments

Comments
 (0)
0