1 year ago

#372027

test-img

JC_CL

How to plot multiple boxplots into one figure?

I have a pandas dataframe with three columns:

df = pd.DataFrame({'first':['A', 'A', 'B', 'C', 'C'],
                   'second': np.random.rand(5),
                   'third': np.random.rand(5)})

I want to plot a box plot for the second and the third colum and group it by the first column. This is all very straigtforward:

df.plot.box(by='first', column='second')
df.plot.box(by='first', column='third')

2 plots

But to make it easier to compare them, I want to plot them both into one plot. Now obviously, that's not going to look nice, and it's gonna be hard to keep them apart, but for the first, the benefits still outwheight the cons (for me) and the second I can solve by coloring it:

color={'boxes': 'r',
       'whiskers': 'DarkOrange',
       'medians': 'k',
       'caps': 'DarkOrange'}
fig,ax = plt.subplots(figsize=(15,10))
df.plot.box(by='first', column='second', ax=ax)
df.plot.box(by='first', column='third', ax=ax, color=color)

1 plot

But there's a few issues, mainly, that I lost my A B C xlabel and while it does plot, these 3 lines error out with ValueError: The number of FixedLocator locations (6), usually from a call to set_ticks, does not match the number of ticklabels (3). It appears that the labels get assigned twice. Looking at other questions that have this error, setting my xlables per hand (adding x_labels = ('A', 'B', 'C') and ...ax=ax, xlabel=x_labels)) should work, but it doesn't.

How do I tell my plot that both boxplots should have the same x labels, and that those are column first?

(Also, proper labels/legend would be nice, but in my case, they aren't that important or will be done in inkscape anyways, but the x-axis is kinda mandatory.)

python

pandas

boxplot

0 Answers

Your Answer

Accepted video resources