Skip to content

Commit

Permalink
FIX: catplot with redundant hue assignment creates empty legend with …
Browse files Browse the repository at this point in the history
…title #3537
  • Loading branch information
abdulwaheedsoudagar committed Nov 1, 2023
1 parent 0a023ff commit fa7edd8
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 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,21 +3093,12 @@ def catplot(
g._update_legend_data(ax)
ax.legend_ = None

if legend and "hue" in p.variables and p.input_format == "long":
# Obtaining the column names for hue, x, and y.
hue_value = p.variables.get("hue")
x_value = p.variables.get("x")
y_value = p.variables.get("y")
# Obtaining the datatype of hue
hue_type = p.var_types.get("hue")
if legend == 'auto' and hue_value in {x_value, y_value} \
and hue_type != 'numeric':
# Setting the title of hue to None if hue matches "x" or
# "y", or if the data type of "hue" is numeric.
title_hue = None
else:
title_hue = hue_value
g.add_legend(title=title_hue, label_order=hue_order)
if legend == "auto":
show_legend = not p._redundant_hue and p.input_format != "wide"
else:
show_legend = bool(legend)

Check warning on line 3099 in seaborn/categorical.py

View check run for this annotation

Codecov / codecov/patch

seaborn/categorical.py#L3099

Added line #L3099 was not covered by tests
if show_legend:
g.add_legend(title=p.variables.get("hue"), label_order=hue_order)

if data is not None:
# Replace the dataframe on the FacetGrid for any subsequent maps
Expand Down

0 comments on commit fa7edd8

Please sign in to comment.