Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion seaborn/_oldcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ def __init__(

data = plotter.plot_data.get("hue", pd.Series(dtype=float))

if data.notna().any():
if data.isna().all():
if palette is not None:
msg = "Ignoring `palette` because no `hue` variable has been assigned."
warnings.warn(msg, stacklevel=4)
else:

map_type = self.infer_map_type(
palette, norm, plotter.input_format, plotter.var_types["hue"]
Expand Down
25 changes: 22 additions & 3 deletions seaborn/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ def _hue_backcompat(self, color, palette, hue_order, force_hue=False):

return palette, hue_order

def _palette_without_hue_backcompat(self, palette, hue_order):
"""Provide one cycle where palette= implies hue= when not provided"""
if "hue" not in self.variables and palette is not None:
msg = "Passing `palette` without assigning `hue` is deprecated."
warnings.warn(msg, FutureWarning, stacklevel=3)
self.legend = False
self.plot_data["hue"] = self.plot_data[self.cat_axis]
self.variables["hue"] = self.variables.get(self.cat_axis)
self.var_types["hue"] = self.var_types.get(self.cat_axis)
hue_order = self.var_levels.get(self.cat_axis)
return hue_order

@property
def cat_axis(self):
return {"v": "x", "h": "y"}[self.orient]
Expand Down Expand Up @@ -352,19 +364,18 @@ def plot_swarms(
points.set_edgecolors(edgecolor)

if not sub_data.empty:
point_collections[sub_data[self.cat_axis].iloc[0]] = points
point_collections[(ax, sub_data[self.cat_axis].iloc[0])] = points

beeswarm = Beeswarm(
width=width, orient=self.orient, warn_thresh=warn_thresh,
)
for center, points in point_collections.items():
for (ax, center), points in point_collections.items():
if points.get_offsets().shape[0] > 1:

def draw(points, renderer, *, center=center):

beeswarm(points, center)

ax = points.axes
if self.orient == "h":
scalex = False
scaley = ax.get_autoscaley_on()
Expand Down Expand Up @@ -2798,6 +2809,7 @@ def stripplot(

p._attach(ax)

hue_order = p._palette_without_hue_backcompat(palette, hue_order)
palette, hue_order = p._hue_backcompat(color, palette, hue_order)

color = _default_color(ax.scatter, hue, color, kwargs)
Expand Down Expand Up @@ -2922,6 +2934,7 @@ def swarmplot(
if not p.has_xy_data:
return ax

hue_order = p._palette_without_hue_backcompat(palette, hue_order)
palette, hue_order = p._hue_backcompat(color, palette, hue_order)

color = _default_color(ax.scatter, hue, color, kwargs)
Expand Down Expand Up @@ -3656,9 +3669,15 @@ def catplot(
if not has_xy_data:
return g

hue_order = p._palette_without_hue_backcompat(palette, hue_order)
palette, hue_order = p._hue_backcompat(color, palette, hue_order)
p.map_hue(palette=palette, order=hue_order, norm=hue_norm)

# Set a default color
# Otherwise each artist will be plotted separately and trip the color cycle
if hue is None and color is None:
color = "C0"

if kind == "strip":

# TODO get these defaults programmatically?
Expand Down
11 changes: 11 additions & 0 deletions tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,15 @@ def test_palette_from_color_deprecation(self, long_df):
for point_color in points.get_facecolors():
assert to_rgb(point_color) in palette

def test_palette_with_hue_deprecation(self, long_df):
palette = "Blues"
with pytest.warns(FutureWarning, match="Passing `palette` without"):
ax = self.func(data=long_df, x="a", y=long_df["y"], palette=palette)
strips = ax.collections
colors = color_palette(palette, len(strips))
for strip, color in zip(strips, colors):
assert same_color(strip.get_facecolor()[0], color)

def test_log_scale(self):

x = [1, 10, 100, 1000]
Expand Down Expand Up @@ -2813,6 +2822,8 @@ def test_plot_elements(self):
g = cat.catplot(x="g", y="y", data=self.df, kind="strip")
want_elements = self.g.unique().size
assert len(g.ax.collections) == want_elements
for strip in g.ax.collections:
assert same_color(strip.get_facecolors(), "C0")

g = cat.catplot(x="g", y="y", hue="h", data=self.df, kind="strip")
want_elements = self.g.unique().size + self.h.unique().size
Expand Down
6 changes: 6 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ def test_hue_map_numeric(self, long_df):
with pytest.raises(ValueError):
HueMapping(p, norm="not a norm")

def test_hue_map_without_hue_dataa(self, long_df):

p = VectorPlotter(data=long_df, variables=dict(x="x", y="y"))
with pytest.warns(UserWarning, match="Ignoring `palette`"):
HueMapping(p, palette="viridis")


class TestSizeMapping:

Expand Down