Skip to content

Commit

Permalink
Fix hue_order as a subset in scatterplot (#2848)
Browse files Browse the repository at this point in the history
Fixes #2590
  • Loading branch information
mwaskom authored Jun 11, 2022
1 parent 94621ce commit 7b3e4df
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/releases/v0.12.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Other updates

- |Fix| Fixed a bug in :class:`PairGrid` where and error would be raised when defining `hue` only in the mapping methods (:pr:`2847`).

- |Fix| Fixed a bug in :func:`scatterplot` where an error would be raised when `hue_order` was a subset of the hue levels (:pr:`2848`).

- |Fix| Subplot titles will no longer be reset when calling :meth:`FacetGrid.map` or :meth:`FacetGrid.map_dataframe` (:pr:`2705`).

- |Fix| In :func:`lineplot`, allowed the `dashes` keyword to set the style of a line without mapping a `style` variable (:pr:`2449`).
Expand Down
7 changes: 7 additions & 0 deletions seaborn/_oldcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ def _lookup_single(self, key):
# Use a value that's in the original data vector
value = self.lookup_table[key]
except KeyError:

if self.norm is None:
# Currently we only get here in scatterplot with hue_order,
# because scatterplot does not consider hue a grouping variable
# So unused hue levels are in the data, but not the lookup table
return (0, 0, 0, 0)

# Use the colormap to interpolate between existing datapoints
# (e.g. in the context of making a continuous legend)
try:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_relational.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from seaborn.external.version import Version
from seaborn.palettes import color_palette
from seaborn._oldcore import categorical_order

from seaborn.relational import (
_RelationalPlotter,
Expand Down Expand Up @@ -1623,6 +1624,16 @@ def test_supplied_color_array(self, long_df):
_draw_figure(ax.figure)
assert_array_equal(ax.collections[0].get_facecolors(), colors)

def test_hue_order(self, long_df):

order = categorical_order(long_df["a"])
unused = order.pop()

ax = scatterplot(data=long_df, x="x", y="y", hue="a", hue_order=order)
points = ax.collections[0]
assert (points.get_facecolors()[long_df["a"] == unused] == 0).all()
assert [t.get_text() for t in ax.legend_.texts] == order

def test_linewidths(self, long_df):

f, ax = plt.subplots()
Expand Down

0 comments on commit 7b3e4df

Please sign in to comment.