Skip to content

Commit 43e20a1

Browse files
committed
refactor(facet): pass scales into compute_layout
1 parent c2367dd commit 43e20a1

5 files changed

Lines changed: 15 additions & 22 deletions

File tree

plotnine/facets/facet.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,6 @@ def __radd__(self, other: ggplot) -> ggplot:
137137
other.facet.environment = other.environment
138138
return other
139139

140-
def axis_positions(self, scales: Scales) -> tuple[str, str]:
141-
"""
142-
The sides the x and y axes occupy, as `(x_side, y_side)`
143-
"""
144-
# `scales.add_missing` adds the default x/y scales *after* the layout
145-
# is computed (see ggplot._build), so scales.x / scales.y can still be
146-
# None here. A missing scale takes the default side of the position
147-
# scale that will replace it: "bottom" for x, "left" for y.
148-
x_side = "bottom" if scales.x is None else scales.x.position
149-
y_side = "left" if scales.y is None else scales.y.position
150-
return x_side, y_side
151-
152140
def setup(self, plot: ggplot):
153141
self.plot = plot
154142
self.layout = plot.layout
@@ -238,7 +226,7 @@ def map(self, data: pd.DataFrame, layout: pd.DataFrame) -> pd.DataFrame:
238226
def compute_layout(
239227
self,
240228
data: list[pd.DataFrame],
241-
axis_positions: tuple[str, str],
229+
scales: Scales,
242230
) -> pd.DataFrame:
243231
"""
244232
Compute layout
@@ -247,8 +235,8 @@ def compute_layout(
247235
----------
248236
data :
249237
Dataframe for a each layer
250-
axis_positions :
251-
The sides the x and y axes occupy, as `(x_side, y_side)`
238+
scales :
239+
The plot's scales
252240
"""
253241
msg = "{} should implement this method."
254242
raise NotImplementedError(msg.format(self.__class__.__name__))

plotnine/facets/facet_grid.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from plotnine.iapi import layout_details
2525
from plotnine.typing import FacetSpaceRatios
2626

27+
from ..scales.scales import Scales
28+
2729

2830
class facet_grid(facet):
2931
"""
@@ -168,7 +170,7 @@ def _make_gridspec(self):
168170
def compute_layout(
169171
self,
170172
data: list[pd.DataFrame],
171-
axis_positions: tuple[str, str],
173+
scales: Scales,
172174
) -> pd.DataFrame:
173175
if not self.rows and not self.cols:
174176
self.nrow, self.ncol = 1, 1
@@ -219,7 +221,7 @@ def compute_layout(
219221
# Relax constraints, if necessary
220222
layout["SCALE_X"] = layout["COL"] if self.free["x"] else 1
221223
layout["SCALE_Y"] = layout["ROW"] if self.free["y"] else 1
222-
x_side, y_side = axis_positions
224+
x_side, y_side = scales.axis_positions
223225
if x_side == "top":
224226
layout["AXIS_X"] = layout["ROW"] == layout["ROW"].min()
225227
else:

plotnine/facets/facet_null.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
if typing.TYPE_CHECKING:
88
import pandas as pd
99

10+
from ..scales.scales import Scales
11+
1012

1113
class facet_null(facet):
1214
"""
@@ -31,6 +33,6 @@ def map(self, data: pd.DataFrame, layout: pd.DataFrame) -> pd.DataFrame:
3133
def compute_layout(
3234
self,
3335
data: list[pd.DataFrame],
34-
axis_positions: tuple[str, str],
36+
scales: Scales,
3537
) -> pd.DataFrame:
3638
return layout_null()

plotnine/facets/facet_wrap.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
from plotnine.iapi import layout_details
2727

28+
from ..scales.scales import Scales
29+
2830

2931
class facet_wrap(facet):
3032
"""
@@ -93,7 +95,7 @@ def __init__(
9395
def compute_layout(
9496
self,
9597
data: list[pd.DataFrame],
96-
axis_positions: tuple[str, str],
98+
scales: Scales,
9799
) -> pd.DataFrame:
98100
if not self.vars:
99101
self.nrow, self.ncol = 1, 1
@@ -136,7 +138,7 @@ def compute_layout(
136138
# Figure out where axes should go.
137139
# The row/column of each panel that shows the axis, on the side the
138140
# axis sits (default: bottom-most row, left-most column)
139-
x_side, y_side = axis_positions
141+
x_side, y_side = scales.axis_positions
140142
if x_side == "top":
141143
x_idx = [df["ROW"].idxmin() for _, df in layout.groupby("COL")]
142144
else:

plotnine/facets/layout.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ def setup(self, layers: Layers, plot: ggplot):
7474

7575
# Generate panel layout
7676
data = self.facet.setup_data(data)
77-
axis_positions = self.facet.axis_positions(plot.scales)
78-
self.layout = self.facet.compute_layout(data, axis_positions)
77+
self.layout = self.facet.compute_layout(data, plot.scales)
7978
self.layout = self.coord.setup_layout(self.layout)
8079
self.check_layout()
8180

0 commit comments

Comments
 (0)