在同一方框图中绘制2个不同的数据帧



因此,我在X和Y中存储了两个不同的数据集。

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

我用下面的代码来绘制

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

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

他把它们并排绘制,但我需要它在同一个盒子上绘制两个不同的数据帧。

我该怎么做?

因为您无论如何都在使用panda,所以这可能是最简单的方法:

# 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()

您仍然可以使用所有常用的matplotlib命令(例如plt.figure(figsize = (15,7))(来自定义绘图。

最新更新