Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a223c7

Browse files
committed
Simplify initial calculations in get_tight_layout_figure.
1 parent 9050dbf commit 6a223c7

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

lib/matplotlib/tight_layout.py

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -277,41 +277,22 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
277277
None if tight_layout could not be accomplished.
278278
"""
279279

280-
subplot_list = []
281-
nrows_list = []
282-
ncols_list = []
283-
ax_bbox_list = []
284-
285-
# Multiple axes can share same subplot_interface (e.g., axes_grid1); thus
286-
# we need to join them together.
287-
subplot_dict = {}
288-
289-
subplotspec_list2 = []
290-
291-
for ax, subplotspec in zip(axes_list, subplotspec_list):
292-
if subplotspec is None:
293-
continue
294-
295-
subplots = subplot_dict.setdefault(subplotspec, [])
296-
297-
if not subplots:
298-
myrows, mycols, _, _ = subplotspec.get_geometry()
299-
nrows_list.append(myrows)
300-
ncols_list.append(mycols)
301-
subplotspec_list2.append(subplotspec)
302-
subplot_list.append(subplots)
303-
ax_bbox_list.append(subplotspec.get_position(fig))
304-
305-
subplots.append(ax)
306-
307-
if len(nrows_list) == 0 or len(ncols_list) == 0:
280+
# Multiple axes can share same subplotspec (e.g., if using axes_grid1);
281+
# we need to group them together.
282+
ss_to_subplots = {ss: [] for ss in subplotspec_list}
283+
for ax, ss in zip(axes_list, subplotspec_list):
284+
ss_to_subplots[ss].append(ax)
285+
ss_to_subplots.pop(None, None) # Skip subplotspec == None.
286+
if not ss_to_subplots:
308287
return {}
288+
subplot_list = list(ss_to_subplots.values())
289+
ax_bbox_list = [ss.get_position(fig) for ss in ss_to_subplots]
309290

310-
max_nrows = max(nrows_list)
311-
max_ncols = max(ncols_list)
291+
max_nrows = max(ss.get_gridspec().nrows for ss in ss_to_subplots)
292+
max_ncols = max(ss.get_gridspec().ncols for ss in ss_to_subplots)
312293

313294
span_pairs = []
314-
for ss in subplotspec_list2:
295+
for ss in ss_to_subplots:
315296
rows, cols = ss.get_gridspec().get_geometry()
316297
div_row, mod_row = divmod(max_nrows, rows)
317298
div_col, mod_col = divmod(max_ncols, cols)
@@ -325,7 +306,6 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
325306
'columns in subplot specifications must be '
326307
'multiples of one another.')
327308
return {}
328-
329309
span_pairs.append((
330310
slice(ss.rowspan.start * div_row, ss.rowspan.stop * div_row),
331311
slice(ss.colspan.start * div_col, ss.colspan.stop * div_col)))

0 commit comments

Comments
 (0)