Skip to content

Commit

Permalink
Avoid error when dodge=True, hue=None (#3605)
Browse files Browse the repository at this point in the history
Fixes #3553
  • Loading branch information
mwaskom authored Dec 27, 2023
1 parent 1617be0 commit 2c115ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions seaborn/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ def _dodge_needed(self):

def _dodge(self, keys, data):
"""Apply a dodge transform to coordinates in place."""
if "hue" not in self.variables:
# Short-circuit if hue variable was not assigned
# We could potentially warn when hue=None, dodge=True, user may be confused
# But I think it's fine to just treat it as a no-op.
return
hue_idx = self._hue_map.levels.index(keys["hue"])
n = len(self._hue_map.levels)
data["width"] /= n
Expand Down
10 changes: 10 additions & 0 deletions tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,16 @@ def test_dodge_native_scale_log(self, long_df):
widths.append(np.ptp(coords))
assert np.std(widths) == approx(0)

def test_dodge_without_hue(self, long_df):

ax = boxplot(long_df, x="a", y="y", dodge=True)
bxp, = ax.containers
levels = categorical_order(long_df["a"])
for i, level in enumerate(levels):
data = long_df.loc[long_df["a"] == level, "y"]
self.check_box(bxp[i], data, "x", i)
self.check_whiskers(bxp[i], data, "x", i)

@pytest.mark.parametrize("orient", ["x", "y"])
def test_log_data_scale(self, long_df, orient):

Expand Down

0 comments on commit 2c115ed

Please sign in to comment.