简体   繁体   中英

Plot 2 different dataframes in the same boxplot

So, I have 2 different datasets stored in X and Y.

x = df1['Sales']
y = df2['Sales']

I'm using the following code to plot them

plt.figure(figsize = (15,7))


plt.subplot(1, 2, 1)
x.plot(kind='box')

plt.subplot(1, 2, 2)
y.plot(kind='box')

And he plots them side by side, but I need it to plot on the same boxplot the 2 different DataFrames.

How can I do that?

Since you're working with pandas anyway, maybe this is the most straightforward approach:

# put both series in one dataframe
df = pd.concat([df1['Sales'], df2['Sales']], axis=1)

# set column names (will be displayed as plot labels)
df.columns = ['x Sales', 'y Sales']  

# use pandas' boxplot method
df.boxplot()

You can still use all the usual matplotlib commands (eg plt.figure(figsize = (15,7)) ) to customize the plot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM