Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 921
Red Sparrow Avatar asked Mar 11 '20 14:03

Red Sparrow


People also ask

What is Violinplot in Seaborn?

A violin plot plays a similar role as a box and whisker plot. It shows the distribution of quantitative data across several levels of one (or more) categorical variables such that those distributions can be compared.

Which parameter is used to add another categorical variable to the violin plot?

The function stat_summary() can be used to add mean/median points and more on a violin plot.

Are violin plots better or worse than box plots?

A violin plot is more informative than a plain box plot. While a box plot only shows summary statistics such as mean/median and interquartile ranges, the violin plot shows the full distribution of the data. The difference is particularly useful when the data distribution is multimodal (more than one peak).

What does a violin plot tell you?

What is a violin plot? A violin plot is a hybrid of a box plot and a kernel density plot, which shows peaks in the data. It is used to visualize the distribution of numerical data. Unlike a box plot that can only show summary statistics, violin plots depict summary statistics and the density of each variable.


1 Answers

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

like image 161
kynnem Avatar answered Nov 09 '22 03:11

kynnem