使用pandas功能绘制多个数据框

问题描述:

我有两个数据框,分别具有x和y坐标,我想在同一图中绘制它们. 我现在在同一图中绘制两个数据框,如下所示:

I have two dataframes, with unique x and y coordinates, and I want to plot them in the same figure. I am now plotting two dataframes in same figure as such:

plt.plot(df1['x'],df1['y'])
plt.plot(df2['x'],df2['y'])
plt.show

但是,熊猫还具有绘图功能.

However, pandas also has plotting functionality.

df.plot()

如何使用熊猫功能实现与第一个示例相同的功能?

How could I achieve the same as my first example but use the pandas functionality?

尝试:

ax = df1.plot()
df2.plot(ax=ax)

基本上pandas的plot函数返回matplotlib对象,然后您可以将其传递给第二个数据框.

Basically pandas' plot function returns the matplotlib object, which you can then pass to the second dataframe.

由J.A. Cado编辑

我想补充一点,我必须为我的代码指定x和y值:

I would like to add that I had to specify the x and y values for my code:

ax = df1.plot(x='Lat', y='Lon')
df2.plot(ax=ax, x='Lat', y='Lon')