How to add hatches on seaborn violin plot?

677 Views Asked by At

If I do something like:

import seaborn as sns 
tips = sns.load_dataset("tips")                                                                                                                                       
ax = sns.violinplot(x="day", y="total_bill", data=tips)

Violin plot with 4 groups in different colors

How can I change the colors by different hatches instead? I tried the same way we do this for boxplot, but didn't work.

1

There are 1 best solutions below

2
On BEST ANSWER

IIUC:

import seaborn as sns
import matplotlib as mpl
tips = sns.load_dataset("tips")
hatch = ['/','\\','+','o']
ihatch = iter(hatch)
ax = sns.violinplot(x="day", y="total_bill", data=tips, color='g')
_ = [i.set_hatch(next(ihatch)) for i in ax.get_children() if isinstance(i, mpl.collections.PolyCollection)]

Output:

enter image description here