首页 > 解决方案 > 在一张图中绘制两个数据框

问题描述

我有这两个数据框 df 和 df1 在哪里

B2B_1 = {'Month': ['10/2018','11/2018','12/2018','01/2019','02/2019','03/2019'],
        'B2B': [200,400, 250,
                100,120,200]]
        }
B2C_1 = {'Month': ['10/2018','11/2018','12/2018','01/2019','02/2019','03/2019'],
        'B2C': [200,400, 250,
                100,120,200]
        }
df=pd.DataFrame(B2B_1)
df1=pd.DataFrame(B2C_1)

而且我不知道如何将这两个数据框绘制在一个轴基于“月份”的图中

标签: pythonpandasmatplotlib

解决方案


pandas绘图函数中有ax参数;如果您将一个例如 matplotlib 轴实例传递给它,它将在该轴上进行绘图。所以

# make a figure and an axis 
fig, ax = plt.subplots(figsize=(14, 7))

# plot desired quantities; note the `ax=ax` in both
df.plot(x="Month", y="B2B", ax=ax)
df1.plot(x="Month", y="B2C", ax=ax)

推荐阅读