|
19 | 19 |
|
20 | 20 | from matplotlib import _api
|
21 | 21 | import matplotlib.transforms as mtransforms
|
| 22 | +import matplotlib._layoutgrid as layoutgrid |
| 23 | + |
22 | 24 |
|
23 | 25 | _log = logging.getLogger(__name__)
|
24 | 26 |
|
@@ -98,6 +100,10 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
|
98 | 100 | 'Possibly did not call parent GridSpec with the'
|
99 | 101 | ' "figure" keyword')
|
100 | 102 |
|
| 103 | + # make layoutgrid tree... |
| 104 | + _layoutgrids = None |
| 105 | + _layoutgrids = _make_layoutgrids(fig, _layoutgrids) |
| 106 | + |
101 | 107 | for _ in range(2):
|
102 | 108 | # do the algorithm twice. This has to be done because decorations
|
103 | 109 | # change size after the first re-position (i.e. x/yticklabels get
|
@@ -128,6 +134,45 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
|
128 | 134 | _reset_margins(fig)
|
129 | 135 |
|
130 | 136 |
|
| 137 | +def _make_layoutgrids(fig, _layoutgrids): |
| 138 | + |
| 139 | + if _layoutgrids is None: |
| 140 | + _layoutgrids = dict() |
| 141 | + if not hasattr(fig, '_parent'): |
| 142 | + # top figure |
| 143 | + _layoutgrids[fig] = layoutgrid.LayoutGrid(parent=None, name='figlb') |
| 144 | + else: |
| 145 | + # subfigure |
| 146 | + gs = fig._subplotspec.get_gridspec() |
| 147 | + parentlb = _layoutgrids[gs] |
| 148 | + if parentlb is not None: |
| 149 | + _layoutgrids[gs] = layoutgrid.LayoutGrid( |
| 150 | + parent=parentlb, |
| 151 | + name=(parentlb.name + '.' + 'panellb' + |
| 152 | + layoutgrid.seq_id()), |
| 153 | + parent_inner=True, |
| 154 | + nrows=1, ncols=1, |
| 155 | + parent_pos=(fig._subplotspec.rowspan, |
| 156 | + fig._subplotspec.colspan)) |
| 157 | + |
| 158 | + for sfig in fig.subfigs: |
| 159 | + _layoutgrids = _make_layoutgrids(sfig, _layoutgrids) |
| 160 | + |
| 161 | + for ax in fig.axes: |
| 162 | + if hasattr(ax, 'get_subplotspec'): |
| 163 | + gs = ax.get_subplotspec().get_gridspec() |
| 164 | + parent = _layoutgrids[gs.figure] |
| 165 | + _layoutgrids[gs] = layoutgrid.LayoutGrid( |
| 166 | + parent=parent, |
| 167 | + parent_inner=True, |
| 168 | + name=(parent.name + '.gridspec' + |
| 169 | + layoutgrid.seq_id()), |
| 170 | + ncols=gs._ncols, nrows=gs._nrows, |
| 171 | + width_ratios=gs.get_width_ratios(), |
| 172 | + height_ratios=gs.get_height_ratios()) |
| 173 | + return _layoutgrids |
| 174 | + |
| 175 | + |
131 | 176 | def _check_no_collapsed_axes(fig):
|
132 | 177 | """
|
133 | 178 | Check that no axes have collapsed to zero size.
|
|
0 commit comments