Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #3537 Disabling the legend based on the x-axis and hue values #3540

Merged
merged 7 commits into from
Nov 4, 2023
8 changes: 5 additions & 3 deletions seaborn/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,10 @@ def _invert_scale(self, ax, data, vars=("x", "y")):
data[col] = inv(data[col])

def _configure_legend(self, ax, func, common_kws=None, semantic_kws=None):

if self.legend == "auto":
show_legend = not self._redundant_hue and self.input_format != "wide"
else:
show_legend = bool(self.legend)

if show_legend:
self.add_legend_data(ax, func, common_kws, semantic_kws=semantic_kws)
handles, _ = ax.get_legend_handles_labels()
Expand Down Expand Up @@ -3095,7 +3093,11 @@ def catplot(
g._update_legend_data(ax)
ax.legend_ = None

if legend and "hue" in p.variables and p.input_format == "long":
if legend == "auto":
show_legend = not p._redundant_hue and p.input_format != "wide"
else:
show_legend = bool(legend)
if show_legend:
g.add_legend(title=p.variables.get("hue"), label_order=hue_order)

if data is not None:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -3125,6 +3125,14 @@ def test_invalid_kind(self, long_df):
with pytest.raises(ValueError, match="Invalid `kind`: 'wrong'"):
catplot(long_df, kind="wrong")

def test_legend_with_auto(self):

g1 = catplot(self.df, x="g", y="y", hue="g", legend='auto')
assert g1._legend is None

g2 = catplot(self.df, x="g", y="y", hue="g", legend=True)
assert g2._legend is not None


class TestBeeswarm:

Expand Down
Loading