54

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?

cottontail
  • 10,268
  • 18
  • 50
  • 51
qwertylpc
  • 2,016
  • 7
  • 24
  • 34
  • What argument are you using to specify alpha on barplot? I can't find anything like that in the docs. – Cody Hess Oct 08 '15 at 03:37
  • 1
    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`. – iayork Oct 08 '15 at 19:02
  • 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})) – Freek Oct 19 '17 at 14:31

2 Answers2

101

Use the scatter_kws argument. For example:

ax = sb.regplot(x="total_bill", 
                y="tip", 
                data=tips, 
                scatter_kws={'alpha':0.3})
iayork
  • 6,420
  • 8
  • 44
  • 49
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

cottontail
  • 10,268
  • 18
  • 50
  • 51