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

Does boxplot support alpha / transparency? #979

Closed
amelio-vazquez-reina opened this issue Jul 25, 2016 · 5 comments
Closed

Does boxplot support alpha / transparency? #979

amelio-vazquez-reina opened this issue Jul 25, 2016 · 5 comments

Comments

@amelio-vazquez-reina
Copy link

The following:

sns.boxplot(data, x='foo', y='bar', alpha=0.3)

Fails with:

TypeError: boxplot() got an unexpected keyword argument 'alpha'

I presume boxplots don't support alpha or transparency? Or is there any other way of getting it?

@amelio-vazquez-reina
Copy link
Author

The above is using 0.7.0 on Python 3.5.2 (conda install on the default channel)

@mwaskom
Copy link
Owner

mwaskom commented Jul 25, 2016

It's not supported through the seaborn API, but it is through kwargs that are passed down to matplotlib:

ax = sns.boxplot(x='day', y='tip', data=tips, boxprops=dict(alpha=.3))

But this also sets the alpha on the edges of the boxes, which I find aesthetically displeasing. A more roundabout way to grab the patch artists after plotting and then to just change the alpha of the box fills:

ax = sns.boxplot(x='day', y='tip', data=tips)
for patch in ax.artists:
    fc = patch.get_facecolor()
    patch.set_facecolor(mpl.colors.to_rgba(fc, 0.3))

there may be a more direct but less obvious way to do that, usually is in matplotlib.

@amelio-vazquez-reina
Copy link
Author

Fantastic. Thanks @mwaskom !

@vinisalazar
Copy link

vinisalazar commented Jun 2, 2022

If you're trying to do this in 2022, change ax.artists for ax.patches, like so:

ax = sns.boxplot(x='day', y='tip', data=tips)
for patch in ax.patches:
    r, g, b, a = patch.get_facecolor()
    patch.set_facecolor((r, g, b, .3))

@mwaskom
Copy link
Owner

mwaskom commented Jun 2, 2022

Specifically, that is a change in matplotlib 3.5.0. If you are using an older matplotlib in 2022, you should still do it the other way :)

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

No branches or pull requests

3 participants