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")
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:
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:
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.
The function stat_summary() can be used to add mean/median points and more on a violin plot.
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 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With