Skip to content

Commit 236770e

Browse files
committed
Build LayoutTree.grid in create to allow DesignGrid
Promote `grid` to a constructor field on `LayoutTree` and move its construction from `__post_init__` into `create`. The latter now picks `DesignGrid` over `Grid` when `cmp._design_spec` is set, in preparation for `plot_layout(design=...)`. No `Compose` writes `_design_spec` yet, so the `DesignGrid` branch is dormant and behavior is unchanged.
1 parent f386696 commit 236770e

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

plotnine/_mpl/layout_manager/_layout_tree.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import numpy as np
88

9-
from ._grid import Grid
9+
from ._grid import DesignGrid, Grid
1010
from ._plot_side_space import PlotSideSpaces
1111

1212
if TYPE_CHECKING:
@@ -124,19 +124,19 @@ class LayoutTree:
124124
represents.
125125
"""
126126

127+
grid: Grid["Node"]
128+
"""
129+
Per-cell layout of `nodes`. `Grid` for compositions without a
130+
design; `DesignGrid` when `plot_layout(design=...)` is used.
131+
"""
132+
127133
sub_gridspec: p9GridSpec = field(init=False, repr=False)
128134
"""
129135
Gridspec (nxn) that contains the composed items
130136
"""
131137

132138
def __post_init__(self):
133139
self.sub_gridspec = self.cmp._sub_gridspec
134-
self.grid = Grid["Node"](
135-
self.nrow,
136-
self.ncol,
137-
self.nodes,
138-
order="row_major" if self.cmp.layout.byrow else "col_major",
139-
)
140140

141141
@property
142142
def ncol(self) -> int:
@@ -172,7 +172,18 @@ def create(cmp: Compose) -> LayoutTree:
172172
else:
173173
nodes.append(LayoutTree.create(item))
174174

175-
return LayoutTree(cmp, nodes)
175+
if (spec := getattr(cmp, "_design_spec", None)) is not None:
176+
grid = DesignGrid["Node"](spec.nrow, spec.ncol, nodes, spec.rects)
177+
else:
178+
order = "row_major" if cmp.layout.byrow else "col_major"
179+
grid = Grid["Node"](
180+
cast("int", cmp.layout.nrow),
181+
cast("int", cmp.layout.ncol),
182+
nodes,
183+
order=order,
184+
)
185+
186+
return LayoutTree(cmp, nodes, grid)
176187

177188
@cached_property
178189
def sub_compositions(self) -> list[LayoutTree]:

0 commit comments

Comments
 (0)