seaborn icon indicating copy to clipboard operation
seaborn copied to clipboard

inconsistent ability to use palette to set alpha

Open Gabriel-Kissin opened this issue 8 months ago • 6 comments

Some plotting functions allow using palette to set the alpha value for a hue - for example, sns.scatterplot.
Other plotting functions however do not allow this - for example, sns.violinplot, sns.barplot.

Demo:

import matplotlib.pyplot as plt
from matplotlib.colors import to_rgba
import seaborn as sns

iris = sns.load_dataset("iris")

colour_dict = {'setosa':         to_rgba('blue', 0.1),
               'versicolor':     to_rgba('blue', 1.0),
               'virginica':      to_rgba('blue', 0.5)}

fig, axs = plt.subplots(ncols=3, figsize=(15,4), sharey=True)
sns.scatterplot(data=iris, x='sepal_width', y='sepal_length', hue='species', palette=colour_dict, ax=axs[0])
sns.barplot(    data=iris,                  y='sepal_length', hue='species', palette=colour_dict, ax=axs[1])
sns.violinplot( data=iris,                  y='sepal_length', hue='species', palette=colour_dict, ax=axs[2])
plt.show()

image

Suggest that all plotting functions allow this, to achieve consistency, and because this can be useful (see https://stackoverflow.com/questions/66667334/python-seaborn-alpha-by-hue).

This may be tricky with e.g. sns.lineplot which apart from the main line also plots a band with a low alpha to show confidence interval. In this case, the specified alpha can be applied to the line, and the band can stay at the default alpha; the line will still be clear due to the additive nature of alpha. Alternatively, at least those plotting functions which don't use alpha could implement this.

https://github.com/mwaskom/seaborn/issues/1966 may be related.

Many thanks for the terrific library :)

Gabriel-Kissin avatar Jun 03 '24 10:06 Gabriel-Kissin