Seaborn: Does boxplot support alpha / transparency?

Created on 25 Jul 2016  路  3Comments  路  Source: mwaskom/seaborn

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?

Most helpful comment

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:
    r, g, b, a = patch.get_facecolor()
    patch.set_facecolor((r, g, b, .3))

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

All 3 comments

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

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:
    r, g, b, a = patch.get_facecolor()
    patch.set_facecolor((r, g, b, .3))

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

Fantastic. Thanks @mwaskom !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amelio-vazquez-reina picture amelio-vazquez-reina  路  4Comments

phantom0301 picture phantom0301  路  3Comments

TDaltonC picture TDaltonC  路  3Comments

stonebig picture stonebig  路  4Comments

btyukodi picture btyukodi  路  3Comments