Skip to content

Commit

Permalink
Fix issue when an empty palette is provided for an empty dataset (#3136)
Browse files Browse the repository at this point in the history
* Fix issue when an empty palette is provided for an empty dataset

* Added test for empty palette with swarmplot or stripplot
  • Loading branch information
thuiop authored Dec 30, 2022
1 parent bf9fcb3 commit 0f5182e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions seaborn/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def _native_width(self):
def _nested_offsets(self, width, dodge):
"""Return offsets for each hue level for dodged plots."""
offsets = None
if "hue" in self.variables:
if "hue" in self.variables and self._hue_map.levels is not None:
n_levels = len(self._hue_map.levels)
if dodge:
each_width = width / n_levels
Expand Down Expand Up @@ -268,7 +268,7 @@ def plot_strips(
jlim = 0.1
else:
jlim = float(jitter)
if "hue" in self.variables and dodge:
if "hue" in self.variables and dodge and self._hue_map.levels is not None:
jlim /= len(self._hue_map.levels)
jlim *= self._native_width
jitterer = partial(np.random.uniform, low=-jlim, high=+jlim)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,9 @@ def test_vs_catplot(self, long_df, wide_df, kwargs):

assert_plots_equal(ax, g.ax)

def test_empty_palette(self):
self.func(x=[], y=[], hue=[], palette=[])


class TestStripPlot(SharedScatterTests):

Expand Down

0 comments on commit 0f5182e

Please sign in to comment.