|
22 | 22 | import matplotlib.transforms as mtransforms
|
23 | 23 |
|
24 | 24 | import numpy as np
|
| 25 | +import warnings |
25 | 26 |
|
26 | 27 | class GridSpecBase(object):
|
27 | 28 | """
|
@@ -241,7 +242,8 @@ def update(self, **kwargs):
|
241 | 242 | ax._sharey.update_params()
|
242 | 243 | ax.set_position(ax._sharey.figbox)
|
243 | 244 | else:
|
244 |
| - if ax.get_subplotspec().get_gridspec() == self: |
| 245 | + ss = ax.get_subplotspec().get_topmost_subplotspec() |
| 246 | + if ss.get_gridspec() == self: |
245 | 247 | ax.update_params()
|
246 | 248 | ax.set_position(ax.figbox)
|
247 | 249 |
|
@@ -287,67 +289,24 @@ def tight_layout(self, fig, renderer=None, pad=1.08, h_pad=None, w_pad=None, rec
|
287 | 289 | labels) will fit into. Default is (0, 0, 1, 1).
|
288 | 290 | """
|
289 | 291 |
|
290 |
| - from tight_layout import auto_adjust_subplotpars, get_renderer |
| 292 | + from tight_layout import (get_subplotspec_list, |
| 293 | + get_tight_layout_figure, |
| 294 | + get_renderer) |
| 295 | + |
| 296 | + subplotspec_list = get_subplotspec_list(fig.axes, grid_spec=self) |
| 297 | + if None in subplotspec_list: |
| 298 | + warnings.warn("This figure includes Axes that are not " |
| 299 | + "compatible with tight_layout, so its " |
| 300 | + "results might be incorrect.") |
291 | 301 |
|
292 | 302 | if renderer is None:
|
293 | 303 | renderer = get_renderer(fig)
|
294 | 304 |
|
295 |
| - subplot_list = [] |
296 |
| - num1num2_list = [] |
297 |
| - subplot_dict = {} |
298 |
| - |
299 |
| - for ax in fig.axes: |
300 |
| - locator = ax.get_axes_locator() |
301 |
| - if hasattr(locator, "get_subplotspec"): |
302 |
| - subplotspec = locator.get_subplotspec() |
303 |
| - elif hasattr(ax, "get_subplotspec"): |
304 |
| - subplotspec = ax.get_subplotspec() |
305 |
| - else: |
306 |
| - continue |
307 |
| - |
308 |
| - if subplotspec.get_gridspec() != self: continue |
309 |
| - |
310 |
| - subplots = subplot_dict.get(subplotspec, []) |
311 |
| - |
312 |
| - if not subplots: |
313 |
| - _, _, num1, num2 = subplotspec.get_geometry() |
314 |
| - num1num2_list.append((num1, num2)) |
315 |
| - subplot_list.append(subplots) |
316 |
| - |
317 |
| - subplots.append(ax) |
318 |
| - |
319 |
| - |
320 |
| - kwargs = auto_adjust_subplotpars(fig, renderer, |
321 |
| - nrows_ncols=self.get_geometry(), |
322 |
| - num1num2_list=num1num2_list, |
323 |
| - subplot_list=subplot_list, |
324 |
| - pad=pad, h_pad=h_pad, w_pad=w_pad) |
325 |
| - |
326 |
| - if rect is not None: |
327 |
| - # if rect is given, the whole subplots area (including |
328 |
| - # labels) will fit into the rect instead of the |
329 |
| - # figure. Note that the rect argument of |
330 |
| - # *auto_adjust_subplotpars* specify the area that will be |
331 |
| - # covered by the total area of axes.bbox. Thus we call |
332 |
| - # auto_adjust_subplotpars twice, where the second run |
333 |
| - # with adjusted rect parameters. |
334 |
| - |
335 |
| - left, bottom, right, top = rect |
336 |
| - if left is not None: left += kwargs["left"] |
337 |
| - if bottom is not None: bottom += kwargs["bottom"] |
338 |
| - if right is not None: right -= (1 - kwargs["right"]) |
339 |
| - if top is not None: top -= (1 - kwargs["top"]) |
340 |
| - |
341 |
| - #if h_pad is None: h_pad = pad |
342 |
| - #if w_pad is None: w_pad = pad |
343 |
| - |
344 |
| - kwargs = auto_adjust_subplotpars(fig, renderer, |
345 |
| - nrows_ncols=self.get_geometry(), |
346 |
| - num1num2_list=num1num2_list, |
347 |
| - subplot_list=subplot_list, |
348 |
| - pad=pad, h_pad=h_pad, w_pad=w_pad, |
349 |
| - rect=(left, bottom, right, top)) |
350 |
| - |
| 305 | + kwargs = get_tight_layout_figure(fig, fig.axes, subplotspec_list, |
| 306 | + renderer, |
| 307 | + pad=pad, h_pad=h_pad, w_pad=w_pad, |
| 308 | + rect=rect, |
| 309 | + ) |
351 | 310 |
|
352 | 311 | self.update(**kwargs)
|
353 | 312 |
|
|
0 commit comments