Search code examples
pythonmatplotlibseabornviolin-plotquartile

Quartiles line properties in seaborn violinplot


trying to figure out how to modify the line properties (color, thickness, style etc) of the quartiles in a seaborn violinplot.

Example code from their website:

import seaborn as sns

sns.set(style="whitegrid")

tips = sns.load_dataset("tips")

ax = sns.violinplot(x="day", y="total_bill", hue="sex",

                    data=tips, palette="Set2", split=True,linestyle=':',

                    scale="count", inner="quartile")

Default violinplot with quartiles

The desired outcome would be to be able to change e.g. the color of the two parts of the violinplot individually as for example like this to improve readability:

Desired output

How can I do this?

Thankful for any insights

UPDATE: Based on the response by @kynnem the following can be used to change the median and quartile lines separately:

import seaborn as sns

sns.set(style="whitegrid")

tips = sns.load_dataset("tips")

ax = sns.violinplot(x="day", y="total_bill", hue="sex",

                    data=tips, palette="Set2", split=True,linestyle=':',

                    scale="count", inner="quartile")
for l in ax.lines:
    l.set_linestyle('--')
    l.set_linewidth(0.6)
    l.set_color('red')
    l.set_alpha(0.8)
for l in ax.lines[1::3]:
    l.set_linestyle('-')
    l.set_linewidth(1.2)
    l.set_color('black')
    l.set_alpha(0.8)

Result:

ViolinModifiedLines


Solution

  • You can access the lines from your ax variable using the following to set line type, color, and saturation:

     for l in ax.lines:
         l.set_linestyle('-')
         l.set_color('black')
         l.set_alpha(0.8)
    

    This creates a solid black line for all horizontal lines. If you can figure out which of the lines in ax correspond with your lines of interest, you can then specify different colors and styles as you wish