0

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?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
p-value
  • 608
  • 8
  • 22
  • couple of things: 1) the 2.5 and 97.5 percentiles of the data are not a confidence interval 2) just use the native `plot` method of the matplotlib Axes object (`ax`, in your case) – Paul H Jan 08 '17 at 16:28
  • 1) Corrected, thanks! Sorry I was abusing the term slightly here... 2) I guess my problem is that I don't know how Seaborn handled categorical values on the x-axis. How do I know the x-value of "Thur", for instance? – p-value Jan 08 '17 at 20:19
  • Axes have a lot methods. Look up `ax.get_xlim()` for starters. – Paul H Jan 08 '17 at 20:31
  • Thanks! I see that "Thur", "Fri", ... actually corresponds to range(3). – p-value Jan 08 '17 at 21:33

1 Answers1

5

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()
Sait
  • 19,045
  • 18
  • 72
  • 99
  • Thanks! I actually just figured out a similar solution, using ax.errorbar, but setting all line_widths to 0 so only the caps remain. – p-value Jan 08 '17 at 21:32
  • can you share how you used error bar without affecting the figure. When ever I use a error bar, the figure rooms line and I cannot control the x axis since they are just labels. – bicepjai Mar 05 '20 at 20:33