Quartiles line properties in seaborn violinplot
Asked Answered
C

1

11

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

Cellophane answered 11/3, 2020 at 14:27 Comment(0)
F
8

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

Faustino answered 31/3, 2020 at 10:7 Comment(2)
Not that I'm aware ofFaustino
Hi @RedSparrow, I'm glad you found my answer helpful. Would you consider accepting it as the answer to your question?Faustino

© 2022 - 2024 — McMap. All rights reserved.