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

Palette setting ignored unless passed directly for numeric hues #3651

Closed
mmatous opened this issue Mar 10, 2024 · 1 comment
Closed

Palette setting ignored unless passed directly for numeric hues #3651

mmatous opened this issue Mar 10, 2024 · 1 comment

Comments

@mmatous
Copy link

mmatous commented Mar 10, 2024

Despite setting colorblind palette globally, seaborn still uses ch:. From the following snippet, only a3 works as expected

MRE:

#!/usr/bin/env python3

import matplotlib.pyplot as plt
import seaborn as sns

sns.set_theme(palette='colorblind')
df = sns.load_dataset('mpg')

a1 = sns.lineplot(x='weight', y='horsepower', hue='cylinders', data=df)
a1.get_figure().savefig('1.svg')
plt.clf()

with sns.color_palette('colorblind'):
    a2 = sns.lineplot(x='weight', y='horsepower', hue='cylinders', data=df)
    a2.get_figure().savefig('2.svg')
    plt.clf()

a3 = sns.lineplot(x='weight', y='horsepower', hue='cylinders', data=df, palette='colorblind')
a3.get_figure().savefig('3.svg')
plt.clf()

Observed output:
observed
Expected output:
expected

For the example above I would expect all three figures to look like the expected file, but the following behavior is observed:

a1 - _base.py:274, numeric_mapping() ignores previously set palette completely and doesn't even try to retrieve any previously set values
a2 - same thing here
a3 - this one works, but _base.py:208 sets the map_type as categorical. That is correct in this case (and for my use-case as well) but I'm not sure about the implications for different data. Well, that's not really a part of this bug report in any case.

I would expect seaborn to handle numeric hue columns so that the result is the same as the second attached figure, generated by a3.

I tried replacing

            palette = "ch:" if palette is None else palette

            if isinstance(palette, mpl.colors.Colormap):
                cmap = palette
            else:
                cmap = color_palette(palette, as_cmap=True)

with something similar to the code in categorical_mapping() but that didn't work because the retrieved structure was a list (or _ColorPalette without as_cmap) and cmap needs to be callable while creating the lookup_table.

SW: Seaborn 0.13.2, Matplotlib 3.8.3

@mwaskom
Copy link
Owner

mwaskom commented Mar 10, 2024

This is expected behavior; the global matplotlib color cycle is used for categorical data, but in most cases it would not make sense to use it for the continuous mapping applied to numeric data.

Because seaborn is currently limited to the global configuration that matplotlib offers, and matplotlib does not distinguish between discrete and continuous color defaults, there's currently no way to set the default palette for numeric data. Although there is already an issue tracking this: #2606

@mwaskom mwaskom closed this as completed Mar 10, 2024
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