2

Extending this question, is there a way to control the transparency of a face of the violinplot without affecting the edge?

When accessing the collection this results only in a reduction in overall alpha (here with increasing transparency taken from the original question):

enter image description here

MonsieurWave
  • 199
  • 3
  • 10

2 Answers2

4

With set_alpha() you change both the face color and the edge color. Instead, you could change just the face color:

import seaborn as sns
from matplotlib.colors import to_rgba

tips = sns.load_dataset("tips")
ax = sns.violinplot(x='day', y='total_bill', data=tips, color='r', linewidth=4)
for violin, alpha in zip(ax.collections[::2], [0.8, 0.6, 0.4, 0.2]):
    violin.set_facecolor(to_rgba(violin.get_facecolor(), alpha=alpha))

using set_facecolor with alpha on violinplot

JohanC
  • 71,591
  • 8
  • 33
  • 66
1

The only way to get different transparency for edge and face has been to access the PolyCollection and individually setting edge and face colors like so:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(10, 2)).melt(var_name='group')
ax = sns.violinplot(data=df, x='group', y='value', inner=None, linewidth=1, saturation=0.5)

# change alpha for edges and faces
ax.collections[0].set_edgecolor((0.9647058823529412, 0.06274509803921569, 0.403921568627451, 1))
ax.collections[0].set_facecolor((0.9647058823529412, 0.06274509803921569, 0.403921568627451, 0.1))

enter image description here

MonsieurWave
  • 199
  • 3
  • 10