Search code examples
pythonseabornviolin-plothatchstyle

How to add hatches on seaborn violin plot?


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.


Solution

  • 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