@@ -23,10 +23,9 @@ def _auto_adjust_subplotpars(
23
23
Return a dict of subplot parameters to adjust spacing between subplots
24
24
or ``None`` if resulting axes would have zero height or width.
25
25
26
- Note that this function ignores geometry information of subplot
27
- itself, but uses what is given by the *nrows_ncols* and *num1num2_list*
28
- parameters. Also, the results could be incorrect if some subplots have
29
- ``adjustable=datalim``.
26
+ Note that this function ignores geometry information of subplot itself, but
27
+ uses what is given by the *shape* and *subplot_list* parameters. Also, the
28
+ results could be incorrect if some subplots have ``adjustable=datalim``.
30
29
31
30
Parameters
32
31
----------
@@ -47,11 +46,11 @@ def _auto_adjust_subplotpars(
47
46
"""
48
47
rows , cols = shape
49
48
50
- font_size_inches = (
49
+ font_size_inch = (
51
50
FontProperties (size = rcParams ["font.size" ]).get_size_in_points () / 72 )
52
- pad_inches = pad * font_size_inches
53
- vpad_inches = h_pad * font_size_inches if h_pad is not None else pad_inches
54
- hpad_inches = w_pad * font_size_inches if w_pad is not None else pad_inches
51
+ pad_inch = pad * font_size_inch
52
+ vpad_inch = h_pad * font_size_inch if h_pad is not None else pad_inch
53
+ hpad_inch = w_pad * font_size_inch if w_pad is not None else pad_inch
55
54
56
55
if len (span_pairs ) != len (subplot_list ) or len (subplot_list ) == 0 :
57
56
raise ValueError
@@ -98,42 +97,37 @@ def _auto_adjust_subplotpars(
98
97
# margins can be negative for axes with aspect applied, so use max(, 0) to
99
98
# make them nonnegative.
100
99
if not margin_left :
101
- margin_left = (max (hspaces [:, 0 ].max (), 0 )
102
- + pad_inches / fig_width_inch )
100
+ margin_left = max (hspaces [:, 0 ].max (), 0 ) + pad_inch / fig_width_inch
103
101
suplabel = fig ._supylabel
104
102
if suplabel and suplabel .get_in_layout ():
105
103
rel_width = fig .transFigure .inverted ().transform_bbox (
106
104
suplabel .get_window_extent (renderer )).width
107
- margin_left += rel_width + pad_inches / fig_width_inch
108
-
105
+ margin_left += rel_width + pad_inch / fig_width_inch
109
106
if not margin_right :
110
- margin_right = (max (hspaces [:, - 1 ].max (), 0 )
111
- + pad_inches / fig_width_inch )
107
+ margin_right = max (hspaces [:, - 1 ].max (), 0 ) + pad_inch / fig_width_inch
112
108
if not margin_top :
113
- margin_top = (max (vspaces [0 , :].max (), 0 )
114
- + pad_inches / fig_height_inch )
109
+ margin_top = max (vspaces [0 , :].max (), 0 ) + pad_inch / fig_height_inch
115
110
if fig ._suptitle and fig ._suptitle .get_in_layout ():
116
111
rel_height = fig .transFigure .inverted ().transform_bbox (
117
112
fig ._suptitle .get_window_extent (renderer )).height
118
- margin_top += rel_height + pad_inches / fig_height_inch
113
+ margin_top += rel_height + pad_inch / fig_height_inch
119
114
if not margin_bottom :
120
- margin_bottom = (max (vspaces [- 1 , :].max (), 0 )
121
- + pad_inches / fig_height_inch )
115
+ margin_bottom = max (vspaces [- 1 , :].max (), 0 ) + pad_inch / fig_height_inch
122
116
suplabel = fig ._supxlabel
123
117
if suplabel and suplabel .get_in_layout ():
124
118
rel_height = fig .transFigure .inverted ().transform_bbox (
125
119
suplabel .get_window_extent (renderer )).height
126
- margin_bottom += rel_height + pad_inches / fig_height_inch
120
+ margin_bottom += rel_height + pad_inch / fig_height_inch
127
121
128
122
if margin_left + margin_right >= 1 :
129
123
_api .warn_external ('Tight layout not applied. The left and right '
130
124
'margins cannot be made large enough to '
131
- 'accommodate all axes decorations. ' )
125
+ 'accommodate all axes decorations.' )
132
126
return None
133
127
if margin_bottom + margin_top >= 1 :
134
128
_api .warn_external ('Tight layout not applied. The bottom and top '
135
129
'margins cannot be made large enough to '
136
- 'accommodate all axes decorations. ' )
130
+ 'accommodate all axes decorations.' )
137
131
return None
138
132
139
133
kwargs = dict (left = margin_left ,
@@ -142,7 +136,7 @@ def _auto_adjust_subplotpars(
142
136
top = 1 - margin_top )
143
137
144
138
if cols > 1 :
145
- hspace = hspaces [:, 1 :- 1 ].max () + hpad_inches / fig_width_inch
139
+ hspace = hspaces [:, 1 :- 1 ].max () + hpad_inch / fig_width_inch
146
140
# axes widths:
147
141
h_axes = (1 - margin_right - margin_left - hspace * (cols - 1 )) / cols
148
142
if h_axes < 0 :
@@ -153,12 +147,12 @@ def _auto_adjust_subplotpars(
153
147
else :
154
148
kwargs ["wspace" ] = hspace / h_axes
155
149
if rows > 1 :
156
- vspace = vspaces [1 :- 1 , :].max () + vpad_inches / fig_height_inch
150
+ vspace = vspaces [1 :- 1 , :].max () + vpad_inch / fig_height_inch
157
151
v_axes = (1 - margin_top - margin_bottom - vspace * (rows - 1 )) / rows
158
152
if v_axes < 0 :
159
153
_api .warn_external ('Tight layout not applied. tight_layout '
160
154
'cannot make axes height small enough to '
161
- 'accommodate all axes decorations' )
155
+ 'accommodate all axes decorations. ' )
162
156
return None
163
157
else :
164
158
kwargs ["hspace" ] = vspace / v_axes
@@ -283,41 +277,28 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
283
277
None if tight_layout could not be accomplished.
284
278
"""
285
279
286
- subplot_list = []
287
- nrows_list = []
288
- ncols_list = []
289
- ax_bbox_list = []
290
-
291
- # Multiple axes can share same subplot_interface (e.g., axes_grid1); thus
292
- # we need to join them together.
293
- subplot_dict = {}
294
-
295
- subplotspec_list2 = []
296
-
297
- for ax , subplotspec in zip (axes_list , subplotspec_list ):
298
- if subplotspec is None :
299
- continue
300
-
301
- subplots = subplot_dict .setdefault (subplotspec , [])
302
-
303
- if not subplots :
304
- myrows , mycols , _ , _ = subplotspec .get_geometry ()
305
- nrows_list .append (myrows )
306
- ncols_list .append (mycols )
307
- subplotspec_list2 .append (subplotspec )
308
- subplot_list .append (subplots )
309
- ax_bbox_list .append (subplotspec .get_position (fig ))
310
-
311
- subplots .append (ax )
312
-
313
- 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 :
314
287
return {}
288
+ subplot_list = list (ss_to_subplots .values ())
289
+ ax_bbox_list = [ss .get_position (fig ) for ss in ss_to_subplots ]
315
290
316
- max_nrows = max (nrows_list )
317
- 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 )
318
293
319
294
span_pairs = []
320
- for ss in subplotspec_list2 :
295
+ for ss in ss_to_subplots :
296
+ # The intent here is to support axes from different gridspecs where
297
+ # one's nrows (or ncols) is a multiple of the other (e.g. 2 and 4),
298
+ # but this doesn't actually work because the computed wspace, in
299
+ # relative-axes-height, corresponds to different physical spacings for
300
+ # the 2-row grid and the 4-row grid. Still, this code is left, mostly
301
+ # for backcompat.
321
302
rows , cols = ss .get_gridspec ().get_geometry ()
322
303
div_row , mod_row = divmod (max_nrows , rows )
323
304
div_col , mod_col = divmod (max_ncols , cols )
@@ -331,7 +312,6 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
331
312
'columns in subplot specifications must be '
332
313
'multiples of one another.' )
333
314
return {}
334
-
335
315
span_pairs .append ((
336
316
slice (ss .rowspan .start * div_row , ss .rowspan .stop * div_row ),
337
317
slice (ss .colspan .start * div_col , ss .colspan .stop * div_col )))
0 commit comments