Vertical lines to separate boxplot groups in python
Asked Answered
P

1

5

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")
Padlock answered 24/2, 2020 at 12:13 Comment(3)
Could you add your code made to use the boxplot?Merchantman
@Padlock I have given an answer using Matplotlib. Does that work for you ? Else can you pls post the o/p of df.to_dict() where df is the dataframe name.Castello
@Castello Thank you for answer. I modified the x vector giving the following values: [0.5,1.5,2.5]. Then for the simple code I provided I am getting separation just between groups.Padlock
C
10

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]]
Castello answered 24/2, 2020 at 12:24 Comment(1)
Thank you for answer. I modified the x vector giving the following values: [0.5,1.5,2.5]. Then for the simple code I provided I am getting separation just between groups.Padlock

© 2022 - 2024 — McMap. All rights reserved.