Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical lines to separate boxplot groups in python

I would like to draw additional vertical lines that will separate grouped boxplots like in the right-hand picture. The left-hand picture is the original picture (source: https://www.originlab.com/doc/Origin-Help/DoubleY-Box-Chart). I would appreciate for help how to do that by using matplotlib or seaborn package. Thank you and best regards.

enter image description here

The simple code I am using to create example boxplot:

fig, ax = plt.subplots(1, sharex=False, sharey=False, gridspec_kw={'hspace': 0}, figsize=(10, 5))
bill = sns.load_dataset("tips")
sns.boxplot(x="day", y="total_bill", hue="sex", data=bill, palette="PRGn")
like image 545
Przem Avatar asked Jan 25 '23 07:01

Przem


1 Answers

You can use this line to draw the vertical lines using matplotlib provided you have created axes or plots. I have put x in [1,2,3,4] as an example you can replace the elements of the list with desired widths.

[ax.axvline(x, color = 'r', linestyle='--') for x in [1,2,3,4]] # you can put your desired colour instead of red.

or

[plt.axvline(x, color = 'r', linestyle='--') for x in [1,2,3,4]]
like image 56
instinct246 Avatar answered Feb 04 '23 21:02

instinct246