Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lmplot/regplot can't set alpha of x_estimator #2538

Closed
milo-trujillo opened this issue Apr 1, 2021 · 3 comments · Fixed by #2853
Closed

lmplot/regplot can't set alpha of x_estimator #2538

milo-trujillo opened this issue Apr 1, 2021 · 3 comments · Fixed by #2853

Comments

@milo-trujillo
Copy link
Contributor

lmplot and regplot both allow setting transparency for either scatterplot points or fit lines, through scatter_kws and line_kws, respectively. However, if x_estimator is set (for example to np.mean), then the scatterplot is replaced by confidence bars and a point at the estimated value. There is currently no way to set the transparency of these confidence bars.

For example:

sns.lmplot(data=example, x="x", y="y", hue="source", x_estimator=np.mean, scatter_kws={'s':20,'alpha':0.3}, line_kws={'alpha':0.3})

Creates an image like:

Opaque confidence bars

Since the two distributions I'm plotting stomp all over one another, I'd really like to make those estimation bars semi-transparent.

I believe the problem comes from the following lines:

# TODO abstraction
ci_kws = {"color": kws["color"]}
ci_kws["linewidth"] = mpl.rcParams["lines.linewidth"] * 1.75
kws.setdefault("s", 50)
xs, ys, cis = self.estimate_data
if [ci for ci in cis if ci is not None]:
for x, ci in zip(xs, cis):
ax.plot([x, x], ci, **ci_kws)
ax.scatter(xs, ys, **kws)

If x_estimator is set, then scatterplot creates a new set of keywords, c_kws, which inherits the color from the user-specified kws, but no other values like alpha.

It seems reasonable to me that the confidence bars and scatterplot should share an alpha value, but if there's a compelling reason to keep those values distinct then it would be great if there were another way to specify the alpha values of those bars.

@jhncls
Copy link

jhncls commented Apr 1, 2021

Here is an attempt to a reproducible example:

import seaborn as sns
import numpy as np
import pandas as pd

example = pd.DataFrame({"x": np.repeat(np.arange(20), 10),
                        "y": np.random.randn(200),
                        "source": np.tile([1, 2], 100)})
sns.lmplot(data=example, x="x", y="y", hue="source", x_estimator=np.mean,
           scatter_kws={'s': 20, 'alpha': 0.3}, line_kws={'alpha': 0.3}, legend=False)

Even the trick to put the transparency into the palette doesn't lead to transparent bars (palette=[to_rgba('C0', 0.3), to_rgba('C1', 0.3)]).

@mwaskom
Copy link
Owner

mwaskom commented Apr 2, 2021

seaborn doesn't as a rule use the alpha channel of values in a color palette, often because alpha is getting set elsewhere.

Matching the error bar alpha to the scatter point alpha seems reasonable.

You can also modify the alpha value by doing something like plt.setp(g.ax.lines, alpha=.3). The presence of the regression lines makes that a bit trickier, though; you are lowering their alpha in your example but I'm not sure if you want that outcome or are just demonstrating that it doesn't produce the effect you want.

@jimouris
Copy link

I face a very similar problem with lineplots. Here's what I have:

ax = sns.lineplot(
  x='x', y='y', hue='source', style='source', markers=True, data=df_bits, 
  err_style="bars", err_kws={ 'elinewidth':2, 'capsize':5, 'capthick':2, }
)

If I add 'alpha' in the err_kws then I get:

TypeError: matplotlib.axes._axes.Axes.errorbar() got multiple values for keyword argument 'alpha'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants