How to make seaborn regplot partially see through (alpha)
Asked Answered
C

2

55

When using seaborn barplot, I can specify an alpha to make the bars semi-translucent. However, when I try this with seaborn regplot, I get an error saying this is an unexpected argument.

I read the documentation online but didn't find much. Could someone point me in the right direction?

Creep answered 8/10, 2015 at 3:1 Comment(3)
What argument are you using to specify alpha on barplot? I can't find anything like that in the docs.Seurat
The barplot docs say "Use plt.bar keyword arguments to further change the aesthetic", meaning it can take the same arguments that matplotlib bar takes, which include alpha.Underplot
But how? Adding plt.bar={'alpha':0.3} does not work (I tried this: sns.barplot(data=data,x=data.index, y='amplicons', color=sns.xkcd_rgb['pale red'], plt.bar={'alpha':0.3}))Disulfide
U
105

Use the scatter_kws argument. For example:

ax = sb.regplot(x="total_bill", 
                y="tip", 
                data=tips, 
                scatter_kws={'alpha':0.3})
Underplot answered 8/10, 2015 at 12:12 Comment(0)
T
1

If we look into its source code, seaborn.regplot() constructs two dictionaries scatter_kws and line_kws which are respectively passed off to calls to matplotlib's scatter() and plot() methods. In other words, the regression line and scatter points are plotted separately using separate kwargs. So for example, the regression line can have different alpha= than scatter points.

In fact, a whole host of kwargs (e.g. marker size, edgecolors, alpha) that matplotlib's scatter() admit are missing from regplot() as a keyword but they can be collected in a dictionary and passed along via the scatter_kws argument. Likewise for kwargs for plot() and line_kws argument.

import seaborn as sns
df = sns.load_dataset('tips')

ax = sns.regplot(x="total_bill", y="tip", data=df, ci=False, 
                 scatter_kws=dict(alpha=0.5, s=30, color='blue', edgecolors='white'),
                 line_kws=dict(alpha=0.7, color='red', linewidth=3))

result

Tantalate answered 18/4, 2023 at 8:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.