Skip to content

Commit 977af0a

Browse files
committed
STP1: set up layoutgrids
1 parent 33e51a5 commit 977af0a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
from matplotlib import _api
2121
import matplotlib.transforms as mtransforms
22+
import matplotlib._layoutgrid as layoutgrid
23+
2224

2325
_log = logging.getLogger(__name__)
2426

@@ -98,6 +100,10 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
98100
'Possibly did not call parent GridSpec with the'
99101
' "figure" keyword')
100102

103+
# make layoutgrid tree...
104+
_layoutgrids = None
105+
_layoutgrids = _make_layoutgrids(fig, _layoutgrids)
106+
101107
for _ in range(2):
102108
# do the algorithm twice. This has to be done because decorations
103109
# 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,
128134
_reset_margins(fig)
129135

130136

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+
131176
def _check_no_collapsed_axes(fig):
132177
"""
133178
Check that no axes have collapsed to zero size.

0 commit comments

Comments
 (0)