@@ -93,8 +93,7 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
9393 """
9494
9595 # make layoutgrid tree...
96- _layoutgrids = None
97- _layoutgrids = _make_layoutgrids (fig , _layoutgrids )
96+ _layoutgrids = _make_layoutgrids (fig , None )
9897 if not _layoutgrids ['hasgrids' ]:
9998 _api .warn_external ('There are no gridspecs with layoutgrids. '
10099 'Possibly did not call parent GridSpec with the'
@@ -134,6 +133,14 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
134133
135134
136135def _make_layoutgrids (fig , _layoutgrids ):
136+ """
137+ Make the layoutgrid tree.
138+
139+ (Sub)Figures get a layoutgrid so we can have figure margins.
140+
141+ Gridspecs that are attached to axes get a layoutgrid so axes
142+ can have margins.
143+ """
137144
138145 if _layoutgrids is None :
139146 _layoutgrids = dict ()
@@ -144,7 +151,10 @@ def _make_layoutgrids(fig, _layoutgrids):
144151 else :
145152 # subfigure
146153 gs = fig ._subplotspec .get_gridspec ()
154+ # its possible the gridspec containing this subfigure hasn't
155+ # been added to the tree yet:
147156 _layoutgrids = _make_layoutgrids_gs (_layoutgrids , gs )
157+ # add the layoutgrid for the subfigure:
148158 parentlb = _layoutgrids [gs ]
149159 _layoutgrids [fig ] = layoutgrid .LayoutGrid (
150160 parent = parentlb ,
@@ -154,9 +164,11 @@ def _make_layoutgrids(fig, _layoutgrids):
154164 nrows = 1 , ncols = 1 ,
155165 parent_pos = (fig ._subplotspec .rowspan ,
156166 fig ._subplotspec .colspan ))
167+ # recursively do all subfigures in this figure...
157168 for sfig in fig .subfigs :
158169 _layoutgrids = _make_layoutgrids (sfig , _layoutgrids )
159170
171+ # for each axes at the local level add its gridspec:
160172 for ax in fig ._localaxes .as_list ():
161173 if hasattr (ax , 'get_subplotspec' ):
162174 gs = ax .get_subplotspec ().get_gridspec ()
@@ -166,9 +178,14 @@ def _make_layoutgrids(fig, _layoutgrids):
166178
167179
168180def _make_layoutgrids_gs (_layoutgrids , gs ):
181+ """
182+ Make the layoutgrid for a gridspec (and anything nested in the gridspec)
183+ """
169184
170185 if gs in _layoutgrids or gs .figure is None :
171186 return _layoutgrids
187+ # in order to do constrained_layout there has to be at least *one*
188+ # gridspec in the tree:
172189 _layoutgrids ['hasgrids' ] = True
173190 if not hasattr (gs , '_subplot_spec' ):
174191 # normal gridspec
@@ -182,17 +199,20 @@ def _make_layoutgrids_gs(_layoutgrids, gs):
182199 width_ratios = gs .get_width_ratios (),
183200 height_ratios = gs .get_height_ratios ())
184201 else :
185- # this is a gridspec inside a subplotspec...
202+ # this is a gridspecfromsubplotspec:
186203 subplot_spec = gs ._subplot_spec
187204 parentgs = subplot_spec .get_gridspec ()
205+ # if its a nested gridspec its possible the parent is not in there yet:
188206 if parentgs not in _layoutgrids :
189207 _layoutgrids = _make_layoutgrids_gs (_layoutgrids , parentgs )
190208 subspeclb = _layoutgrids [parentgs ]
191- _layoutgrids [str (gs )+ 'top' ] = layoutgrid .LayoutGrid (
192- parent = subspeclb ,
193- name = subspeclb .name + '.top' + layoutgrid .seq_id (),
194- nrows = 1 , ncols = 1 ,
195- parent_pos = (subplot_spec .rowspan , subplot_spec .colspan ))
209+ # gridspecfromsubplotspec need an outer container:
210+ if str (gs )+ 'top' not in _layoutgrids :
211+ _layoutgrids [str (gs )+ 'top' ] = layoutgrid .LayoutGrid (
212+ parent = subspeclb ,
213+ name = subspeclb .name + '.top' + layoutgrid .seq_id (),
214+ nrows = 1 , ncols = 1 ,
215+ parent_pos = (subplot_spec .rowspan , subplot_spec .colspan ))
196216 _layoutgrids [gs ] = layoutgrid .LayoutGrid (
197217 parent = _layoutgrids [str (gs )+ 'top' ],
198218 name = (_layoutgrids [str (gs )+ 'top' ].name + '.gridspec' +
0 commit comments