Skip to content

Commit

Permalink
Rename hue -> color in semantic mapping code
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Waskom committed Sep 11, 2021
1 parent c021779 commit 8e87e28
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
19 changes: 10 additions & 9 deletions seaborn/_core/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __call__(self, x): # TODO types; will need to overload (wheee)
# Against:
# Our current external interface consumes both mapping parameterization like the
# color palette to use and the order information. I think this makes a fair amount
# of sense. But we could also break those, e.g. have `scale_fixed("hue", order=...)`
# of sense. But we could also break those, e.g. have `scale_fixed("color", order=...)`
# similar to what we are currently developing for the x/y. Is is another method call
# which may be annoying. But then alternately it is maybe more consistent (and would
# consistently hook into whatever internal representation we'll use for variable order).
Expand All @@ -65,7 +65,7 @@ def setup(self, data: Series, scale: Scale | None = None) -> GroupMapping:
return self


class HueMapping(SemanticMapping):
class ColorMapping(SemanticMapping):
"""Mapping that sets artist colors according to data values."""

# TODO type the important class attributes here
Expand All @@ -78,7 +78,7 @@ def setup(
self,
data: Series, # TODO generally rename Series arguments to distinguish from DF?
scale: Scale | None = None, # TODO or always have a Scale?
) -> HueMapping:
) -> ColorMapping:
"""Infer the type of mapping to use and define it using this vector of data."""
palette: PaletteSpec = self._input_palette
cmap: Colormap | None = None
Expand Down Expand Up @@ -161,7 +161,7 @@ def _setup_categorical(
palette: PaletteSpec,
order: list | None,
) -> tuple[list, dict]:
"""Determine colors when the hue mapping is categorical."""
"""Determine colors when the mapping is categorical."""
# -- Identify the order and name of the levels

levels = categorical_order(data, order)
Expand Down Expand Up @@ -203,14 +203,15 @@ def _setup_numeric(
palette: PaletteSpec,
norm: Normalize | None,
) -> tuple[list, dict, Normalize | None, Colormap]:
"""Determine colors when the hue variable is quantitative."""
"""Determine colors when the variable is quantitative."""
cmap: Colormap
if isinstance(palette, dict):

# The presence of a norm object overrides a dictionary of hues
# in specifying a numeric mapping, so we need to process it here.
# In the function interface, the presence of a norm object overrides
# a dictionary of colors to specify a numeric mapping, so we need
# to process it here.
# TODO this functionality only exists to support the old relplot
# hack for linking hue orders across facets. We don't need that any
# hack for linking hue orders across facets. We don't need that any
# more and should probably remove this, but needs deprecation.
# (Also what should new behavior be? I think an error probably).
levels = list(sorted(palette))
Expand Down Expand Up @@ -241,7 +242,7 @@ def _setup_numeric(
elif isinstance(norm, tuple):
norm = mpl.colors.Normalize(*norm)
elif not isinstance(norm, mpl.colors.Normalize):
err = "`hue_norm` must be None, tuple, or Normalize object."
err = "`norm` must be None, tuple, or Normalize object."
raise ValueError(err)
norm.autoscale_None(data.dropna())

Expand Down
11 changes: 7 additions & 4 deletions seaborn/_core/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from seaborn._core.rules import categorical_order, variable_type
from seaborn._core.data import PlotData
from seaborn._core.subplots import Subplots
from seaborn._core.mappings import GroupMapping, HueMapping
from seaborn._core.mappings import GroupMapping, ColorMapping
from seaborn._core.scales import (
ScaleWrapper,
CategoricalScale,
Expand Down Expand Up @@ -63,7 +63,7 @@ def __init__(
# empty and define the defaults elsewhere
self._mappings = {
"group": GroupMapping(),
"hue": HueMapping(),
"color": ColorMapping(),
}

# TODO is using "unknown" here the best approach?
Expand Down Expand Up @@ -219,6 +219,7 @@ def pair(

def facet(
self,
# TODO require kwargs?
col: VariableSpec = None,
row: VariableSpec = None,
col_order: OrderSpec = None, # TODO single order param
Expand Down Expand Up @@ -249,15 +250,17 @@ def facet(

return self

def map_hue(
def map_color(
self,
palette: PaletteSpec = None,
) -> Plot:

# TODO we do some fancy business currently to avoid having to
# write these ... do we want that to persist or is it too confusing?
# If we do ... maybe we don't even need to write these methods, but can
# instead programatically add them based on central dict of mapping objects.
# ALSO TODO should these be initialized with defaults?
self._mappings["hue"] = HueMapping(palette)
self._mappings["color"] = ColorMapping(palette)
return self

# TODO originally we had planned to have a scale_native option that would default
Expand Down
25 changes: 13 additions & 12 deletions seaborn/_marks/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Point(Mark):

supports = ["hue"]
supports = ["color"]

def __init__(self, jitter=None, **kwargs):

Expand Down Expand Up @@ -41,11 +41,11 @@ def _plot_split(self, keys, data, ax, mappings, kws):

# TODO since names match, can probably be automated!
# TODO note that newer style is to modify the artists
if "hue" in data:
c = mappings["hue"](data["hue"])
if "color" in data:
c = mappings["color"](data["color"])
else:
# TODO prevents passing in c. But do we want to permit that?
# I think if we implement map_hue("identity"), then no
# I think if we implement map_color("identity"), then no
c = None

# TODO Not backcompat with allowed (but nonfunctional) univariate plots
Expand All @@ -58,26 +58,27 @@ class Line(Mark):
# i.e. Line needs to aggregate by x, but not plot by it
# also how will this get parametrized to support orient=?
# TODO will this sort by the orient dimension like lineplot currently does?
grouping_vars = ["hue", "size", "style"]
supports = ["hue"]
grouping_vars = ["color", "size", "style"]
supports = ["color"]

def _plot_split(self, keys, data, ax, mappings, kws):

if "hue" in keys:
kws["color"] = mappings["hue"](keys["hue"])
if "color" in keys:
kws["color"] = mappings["color"](keys["color"])

ax.plot(data["x"], data["y"], **kws)


class Area(Mark):

grouping_vars = ["hue"]
supports = ["hue"]
grouping_vars = ["color"]
supports = ["color"]

def _plot_split(self, keys, data, ax, mappings, kws):

if "hue" in keys:
kws["facecolor"] = mappings["hue"](keys["hue"])
if "color" in keys:
# TODO as we need the kwarg to be facecolor, that should be the mappable?
kws["facecolor"] = mappings["color"](keys["color"])

# TODO how will orient work here?
# Currently this requires you to specify both orient and use y, xmin, xmin
Expand Down

0 comments on commit 8e87e28

Please sign in to comment.