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

catplot with numeric hue and hue_order: empty legend handles #3639

Open
jhncls opened this issue Feb 27, 2024 · 2 comments
Open

catplot with numeric hue and hue_order: empty legend handles #3639

jhncls opened this issue Feb 27, 2024 · 2 comments

Comments

@jhncls
Copy link

jhncls commented Feb 27, 2024

Tested with Seaborn 0.13.2, pandas 2.2.1

When the hue values are numeric, hue_order isn't respected in the plot. The legend does respect the order, but with empty legend handles.

import seaborn as sns
import pandas as pd

tips = sns.load_dataset('tips')
sns.catplot(tips, kind='box', x='time', y='tip', hue='size', hue_order=[2, 3, 4])

image

Making the hue column of type pd.Categorical, but still numeric, does respect the hue order. But again with empty legend handles. The default palette changes to categorical.

import seaborn as sns
import pandas as pd

tips = sns.load_dataset('tips')
tips['size'] = pd.Categorical(tips['size'])
sns.catplot(tips, kind='box', x='time', y='tip', hue='size', hue_order=[2, 3, 4])

image

@mwaskom
Copy link
Owner

mwaskom commented Mar 1, 2024

When the hue values are numeric, hue_order isn't respected in the plot.

This is expected

The legend does respect the order, but with empty legend handles.

This is probably an artifact of the different handling for the catplot legend

@jhncls
Copy link
Author

jhncls commented Mar 1, 2024

The numeric hue values behave a bit unexpected when somebody wanted to superimpose a stripplot on a boxplot via a catplot. The plots don't align if not all hue values are present in all subplots. It only aligns well for non-numeric hue values of type pd.Categorical.

import seaborn as sns
tips = sns.load_dataset('tips')
common_kws = dict(x='time', y='tip', hue='size', dodge=True, palette='turbo')
g = sns.catplot(tips, kind='box', col='smoker', fill=False, **common_kws)
for s, ax in g.axes_dict.items():
    sns.stripplot(tips[tips['smoker'] == s], s=8, legend=False, ax=ax, **common_kws)

image

Anyway, it is a bit surprising that hue_order can be used for subsets, supersets and ordering, but only for strings. (I am not asking to make any changes. I understand it is complicated, where people sometimes interpret numbers as discrete and sometimes as continuous.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants