Plot Additional Quantiles on Seaborn Violin Plots

3.8k views Asked by At

Using the example on http://seaborn.pydata.org/generated/seaborn.violinplot.html:

import seaborn as sns
sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
ax = sns.violinplot(x="day", y="total_bill", data=tips)

Violin plot
(source: pydata.org)

How can I draw two small horizontal lines on top of each violin (like the caps of error bars indicating the 2.5 percentile and the 97.5 percentile of the distribution?

1

There are 1 answers

2
Sait On

Here is a rather hacky solution:

What about drawing another boxplot on top of your Violin plot? (And hiding the box in the box plot.)

Here is the output using 2.5 and 97.5:

enter image description here

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_style("whitegrid")
tips = sns.load_dataset("tips")

sns.boxplot(x="day", y="total_bill", data=tips, showfliers=False, showbox=False, whis=[2.5,97.5])
sns.violinplot(x="day", y="total_bill", data=tips)

plt.show()