question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValueError: The palette list has the wrong number of colors.

See original GitHub issue

Given a DataFrame with two categories:

df = pd.DataFrame({"a": [1,2,3,4,5,6,7,8,9,10], "b": ["a", "b", "a", "b", "a", "b", "a", "b", "a", "b", ]})

When I provide a palette with lineplot, I get an error:

sns.lineplot(data=df, x="a", y="a", hue="b", palette=sns.color_palette())

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-4e6a1a2e63cf> in <module>
----> 1 sns.lineplot(data=df, x="a", y="a", hue="b", palette=sns.color_palette())

/data1/mschroeder/miniconda3/envs/20-assdc/lib/python3.7/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
     44             )
     45         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46         return f(**kwargs)
     47     return inner_f
     48 

/data1/mschroeder/miniconda3/envs/20-assdc/lib/python3.7/site-packages/seaborn/relational.py in lineplot(x, y, hue, size, style, data, palette, hue_order, hue_norm, sizes, size_order, size_norm, dashes, markers, style_order, units, estimator, ci, n_boot, seed, sort, err_style, err_kws, legend, ax, **kwargs)
    696     )
    697 
--> 698     p.map_hue(palette=palette, order=hue_order, norm=hue_norm)
    699     p.map_size(sizes=sizes, order=size_order, norm=size_norm)
    700     p.map_style(markers=markers, dashes=dashes, order=style_order)

/data1/mschroeder/miniconda3/envs/20-assdc/lib/python3.7/site-packages/seaborn/_core.py in map(cls, plotter, *args, **kwargs)
     51         # This method is assigned the __init__ docstring
     52         method_name = "_{}_map".format(cls.__name__[:-7].lower())
---> 53         setattr(plotter, method_name, cls(plotter, *args, **kwargs))
     54         return plotter
     55 

/data1/mschroeder/miniconda3/envs/20-assdc/lib/python3.7/site-packages/seaborn/_core.py in __init__(self, plotter, palette, order, norm)
    117                 cmap = norm = None
    118                 levels, lookup_table = self.categorical_mapping(
--> 119                     data, palette, order,
    120                 )
    121 

/data1/mschroeder/miniconda3/envs/20-assdc/lib/python3.7/site-packages/seaborn/_core.py in categorical_mapping(self, data, palette, order)
    202                 if len(palette) != n_colors:
    203                     err = "The palette list has the wrong number of colors."
--> 204                     raise ValueError(err)
    205                 colors = palette
    206             else:

ValueError: The palette list has the wrong number of colors.

With pointplot, this works fine (it uses the first two colors of the provided palette):

sns.pointplot(data=df, x="a", y="a", hue="b", palette=sns.color_palette())

I think the behavior of pointplot is the correct one.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mwaskomcommented, Sep 22, 2021

I definitely agree that it would be ideal if all functions used the same rules for handling (and error-checking) their inputs. Currently lineplot and pointplot don’t go through the same code path, and the path forward there is to refactor the internals of the categorical plots so that they can (#2429).

As for how to handle cases like these, I think I am currently leaning towards saying that there should be an informative warning when a len(palette) != len(hue_levels), rather than an error. That will need to be the case anyway for the categorical plots, at least temporarily, because we can’t just flip a switch and make inputs that used to be happily accepted error (unless a strong case could be made that that behavior was buggy).

1reaction
mwaskomcommented, Sep 17, 2021

But why argue?

Because there is always more to do in open source than there is time to do it, so it is important to be clear for the purposes of prioritization. Also, because issues that confuse “this is incorrect because it does not work as intended” and “this is incorrect because it does not work as i would prefer” are an irritation that, over time, drains one’s interest in working on any of them.

In any case,

And here, the the palette behavior of pointplot is a superset of the behavior of lineplot, but much more flexible.

I don’t agree. First, I think you’re overstating the apparent limitation. You’re already making a custom list, all you need to do is add one parameter so that it has the correct number of colors.

Second, the error is strict but also prevents you from making unintended mistakes. In your particular case it appears that you are not at risk of problems but if you were passing a color list that had fewer distinct values than you needed, it would have to cycle and you’d get a plot that can’t be clearly deciphered. And if you were using a gradient color palette, taking the first few colors would give you less contrast than you’d get if you generated the palette with the right length to begin with:

f, axs = plt.subplots(1, 2)
pal = "viridis"
sns.lineplot(data=tips, x="day", y="total_bill", hue="smoker", palette=sns.color_palette(pal)[:2], ax=axs[0])
sns.lineplot(data=tips, x="day", y="total_bill", hue="smoker", palette=pal, ax=axs[1])
f.tight_layout()

image

Again I am open to the idea that the error is too constraining and that this should be at most a warning. But there are considerations that you might not have thought of because they don’t bear on your very specific usecase.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Seaborn Color Palette not working appropiate with lineplot
The problem is, that I get the following error: ValueError: The palette list has the wrong number of colors. So the question is:...
Read more >
seaborn.color_palette — seaborn 0.12.1 documentation
Named palettes default to 6 colors, but grabbing the current palette or passing in a list of colors will not change the number...
Read more >
How to use your own color palettes with Seaborn
1. Using an existing color palette. seaborn and matplotlib have a lot of different color palettes to choose from. In this example I'm...
Read more >
Seaborn Color Palette not working appropiate with lineplot
Below you can see the code I'm using. The problem is, that I get the following error: ValueError: The palette list has the...
Read more >
Seaborn: Color Palettes - ProgramsBuzz
Scatter Plot from the seaborn library is used to plot the data. This plot has been used to see whether color palette gets...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found