Skip to content

Commit

Permalink
Make scale a required parameter of mapping setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaskom committed Oct 13, 2021
1 parent 81482fd commit c7777d9
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 75 deletions.
8 changes: 4 additions & 4 deletions seaborn/_core/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _homogenize_values(self, values):
def setup(
self,
data: Series,
scale: Scale | None = None,
scale: Scale,
) -> SemanticMapping:

raise NotImplementedError()
Expand Down Expand Up @@ -117,7 +117,7 @@ def _default_values(self, n: int) -> list:
def setup(
self,
data: Series,
scale: Scale | None = None,
scale: Scale,
) -> LookupMapping:

values = self._values
Expand Down Expand Up @@ -186,7 +186,7 @@ def _infer_map_type(
def setup(
self,
data: Series,
scale: Scale | None = None,
scale: Scale,
) -> NormedMapping | LookupMapping:

values = self.default_range if self._values is None else self._values
Expand Down Expand Up @@ -265,7 +265,7 @@ def __init__(self, palette: PaletteSpec = None, variable: str = "color"):
def setup(
self,
data: Series,
scale: Scale | None = None,
scale: Scale,
) -> LookupMapping | NormedMapping:
"""Infer the type of mapping to use and define it using this vector of data."""
mapping: LookupMapping | NormedMapping
Expand Down
7 changes: 3 additions & 4 deletions seaborn/_core/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import matplotlib as mpl
import matplotlib.pyplot as plt # TODO defer import into Plot.show()

from seaborn._core.rules import variable_type, categorical_order
from seaborn._core.rules import categorical_order
from seaborn._core.data import PlotData
from seaborn._core.subplots import Subplots
from seaborn._core.mappings import (
Expand Down Expand Up @@ -575,10 +575,9 @@ def _setup_scales(self) -> None:
# Because we only want to concat if a variable was *added* here
*(y.data.frame.get(var) for y in self._layers if var in y.variables)
], ignore_index=True)
var_type = variable_type(all_values)

# TODO eventually this will be updating a different dictionary
self._scales[var] = ScaleWrapper.from_inferred_type(var_type)
self._scales[var] = ScaleWrapper.from_inferred_type(all_values)

# TODO Think about how this is going to handle situations where we have
# e.g. ymin and ymax but no y specified. I think in that situation one
Expand Down Expand Up @@ -828,7 +827,7 @@ def _scale_coords_single(
# TODO FIXME:feedback wrap this in a try/except and reraise with
# more information about what variable caused the problem
values = scale.cast(values)
axis_obj.update_units(categorical_order(values))
axis_obj.update_units(categorical_order(values)) # TODO think carefully

# TODO it seems wrong that we need to cast to float here,
# but convert_units sometimes outputs an object array (e.g. w/Int64 values)
Expand Down
14 changes: 9 additions & 5 deletions seaborn/_core/scales.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ def __deepcopy__(self, memo=None):
return copy(self)

@classmethod
def from_inferred_type(cls, var_type: VarType):
def from_inferred_type(cls, data: Series) -> ScaleWrapper:

var_type = variable_type(data)
axis = data.name
if var_type == "numeric":
return cls(LinearScale(), "numeric", None)
scale = cls(LinearScale(axis), "numeric", None)
elif var_type == "categorical":
return cls(CategoricalScale(), "categorical", None)
scale = cls(CategoricalScale(axis), "categorical", None)
elif var_type == "datetime":
# TODO add DateTimeNorm that converts to numeric first
return cls(DatetimeScale(), "datetime", None)
scale = cls(DatetimeScale(axis), "datetime", None)
scale.type_declared = False
return scale

@property
def order(self):
Expand Down Expand Up @@ -90,7 +94,7 @@ class CategoricalScale(LinearScale):

def __init__(
self,
axis: str | None = None,
axis: str,
order: list | None = None,
formatter: Any = None
):
Expand Down
Loading

0 comments on commit c7777d9

Please sign in to comment.