Skip to content

Commit

Permalink
FIX #3537 Disabling the legend based on the x-axis and hue values (#3540
Browse files Browse the repository at this point in the history
)

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

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

* FIX: catplot with redundant hue assignment creates empty legend with title #3537

* FIX: catplot with redundant hue assignment creates empty legend with title #3537

* FIX: catplot with redundant hue assignment creates empty legend with title #3537

* FIX: catplot with redundant hue assignment creates empty legend with title #3537

* FIX: catplot with redundant hue assignment creates empty legend with title #3537
  • Loading branch information
abdulwaheedsoudagar authored and mwaskom committed Nov 4, 2023
1 parent ed63bda commit c2cdb7d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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 @@ -3097,7 +3095,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

0 comments on commit c2cdb7d

Please sign in to comment.