seaborn icon indicating copy to clipboard operation
seaborn copied to clipboard

Alpha values of RGBA color tuples are ignored

Open Huii opened this issue 5 years ago • 8 comments

Thank you very much for providing this awesome library. Unfortunately, I found a bug concerning the handling of the transparency values in RGBA colour tuples. The alpha value is simply being ignored, as

print(sns.color_palette([(1,1,1,0)])) only results in the following output:

[(1.0, 1.0, 1.0)]

Huii avatar Feb 19 '20 15:02 Huii

It is not a bug, in the sense that the behavior matches what is documented:

Returns
    palette: list of RGB tuples

One might wish that RGBA input yields RGBA output, which might be reasonable. But there are many matplotlib functions where the alpha channel of the color input is ignored and the opacity is controlled globally by a separate parameter. So in most cases, this would not work as well as expected.

mwaskom avatar Feb 24 '20 16:02 mwaskom

Thank you very much for your reply. I would still prefer to be able to set the alpha value myself. Otherwise, I wouldn't, for example, be able to create a scatterplot with varying degrees of transparency for different categories.

Related to that, it would maybe be a good idea to change the following line of code: https://github.com/mwaskom/seaborn/blob/88479dc3a9c5dee00fa697fda73152add0827fc4/seaborn/relational.py#L937 to allow for setting the alpha value in relation to the hue value. What's your opinion on that?

Huii avatar Feb 25 '20 09:02 Huii

But there are many matplotlib functions where the alpha channel of the color input is ignored and the opacity is controlled globally by a separate parameter. So in most cases, this would not work as well as expected.

In matplotlib the alpha parameter takes precendence over individual color's alpha channel; however, if you don't specify a global alpha, it will obey to full RGBA throughout. If there is a case where it doesn't, it should be reported as bug.

ImportanceOfBeingErnest avatar Feb 28 '20 00:02 ImportanceOfBeingErnest

Yes I misspoke; I meant there are seaborn functions that work that way.

Part of the challenge is that some seaborn functions (e.g. lineplot with an error band, kdeplot with shaded density) use the color for multiple elements but explicitly set different alpha channels. In other functions (e.g. boxplot or violinplot), the color set by the palette applies only to one matplotlib element, but you might want more global control over the opacity of the whole plot.

So, it's tricky.

mwaskom avatar Mar 17 '20 22:03 mwaskom

I would also ask for the same functionality. I am facing this using stripplot

aliechoes avatar Mar 23 '22 17:03 aliechoes

Would also be useful for barplot if you want to overlay two plots and use alpha to highlight or annotate certain sections of the original plot. Non-standard usage, I know, but still would be good for me.

pollackscience avatar Mar 24 '22 15:03 pollackscience

FWIW, I'd like this as well; my use case is for stripplot, where there are too many data points to effectively use swarmplot. Using stripplot with a low alpha allows a 'cloud' effect to give some idea of density to non-stats people for whom a violin plot would be too much. But, there is another category of points that are unusual and rare, so I want them to be opaque. Unfortunately, I discovered (after much frustration) that the alpha settings in the colors provided to the palette argument are ignored.

Even documenting this behavior would be useful, as one would not keep experimenting, trying to figure out what they were doing wrong.

NickFabry avatar Sep 24 '22 00:09 NickFabry

I subscribe also to this feature request.

It is something that is missing in the current seaborn package

marquez85 avatar Sep 24 '22 01:09 marquez85