@@ -93,8 +93,7 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
93
93
"""
94
94
95
95
# make layoutgrid tree...
96
- _layoutgrids = None
97
- _layoutgrids = _make_layoutgrids (fig ,_layoutgrids )
96
+ _layoutgrids = _make_layoutgrids (fig ,None )
98
97
if not _layoutgrids ['hasgrids' ]:
99
98
_api .warn_external ('There are no gridspecs with layoutgrids. '
100
99
'Possibly did not call parent GridSpec with the'
@@ -134,6 +133,14 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
134
133
135
134
136
135
def _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
+ """
137
144
138
145
if _layoutgrids is None :
139
146
_layoutgrids = dict ()
@@ -144,7 +151,10 @@ def _make_layoutgrids(fig, _layoutgrids):
144
151
else :
145
152
# subfigure
146
153
gs = fig ._subplotspec .get_gridspec ()
154
+ # its possible the gridspec containing this subfigure hasn't
155
+ # been added to the tree yet:
147
156
_layoutgrids = _make_layoutgrids_gs (_layoutgrids ,gs )
157
+ # add the layoutgrid for the subfigure:
148
158
parentlb = _layoutgrids [gs ]
149
159
_layoutgrids [fig ]= layoutgrid .LayoutGrid (
150
160
parent = parentlb ,
@@ -154,9 +164,11 @@ def _make_layoutgrids(fig, _layoutgrids):
154
164
nrows = 1 ,ncols = 1 ,
155
165
parent_pos = (fig ._subplotspec .rowspan ,
156
166
fig ._subplotspec .colspan ))
167
+ # recursively do all subfigures in this figure...
157
168
for sfig in fig .subfigs :
158
169
_layoutgrids = _make_layoutgrids (sfig ,_layoutgrids )
159
170
171
+ # for each axes at the local level add its gridspec:
160
172
for ax in fig ._localaxes .as_list ():
161
173
if hasattr (ax ,'get_subplotspec' ):
162
174
gs = ax .get_subplotspec ().get_gridspec ()
@@ -166,9 +178,14 @@ def _make_layoutgrids(fig, _layoutgrids):
166
178
167
179
168
180
def _make_layoutgrids_gs (_layoutgrids ,gs ):
181
+ """
182
+ Make the layoutgrid for a gridspec (and anything nested in the gridspec)
183
+ """
169
184
170
185
if gs in _layoutgrids or gs .figure is None :
171
186
return _layoutgrids
187
+ # in order to do constrained_layout there has to be at least *one*
188
+ # gridspec in the tree:
172
189
_layoutgrids ['hasgrids' ]= True
173
190
if not hasattr (gs ,'_subplot_spec' ):
174
191
# normal gridspec
@@ -182,17 +199,20 @@ def _make_layoutgrids_gs(_layoutgrids, gs):
182
199
width_ratios = gs .get_width_ratios (),
183
200
height_ratios = gs .get_height_ratios ())
184
201
else :
185
- # this is agridspec inside a subplotspec...
202
+ # this is agridspecfromsubplotspec:
186
203
subplot_spec = gs ._subplot_spec
187
204
parentgs = subplot_spec .get_gridspec ()
205
+ # if its a nested gridspec its possible the parent is not in there yet:
188
206
if parentgs not in _layoutgrids :
189
207
_layoutgrids = _make_layoutgrids_gs (_layoutgrids ,parentgs )
190
208
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 ))
196
216
_layoutgrids [gs ]= layoutgrid .LayoutGrid (
197
217
parent = _layoutgrids [str (gs )+ 'top' ],
198
218
name = (_layoutgrids [str (gs )+ 'top' ].name + '.gridspec' +