使用 Pandas 功能绘制多个数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37414008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:17:00  来源:igfitidea点击:

Plotting multiple dataframes using pandas functionality

pythonpandasmatplotlib

提问by J.A.Cado

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:

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

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

However, pandas also has plotting functionality.

然而,pandas 也有绘图功能。

df.plot()

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

我怎样才能实现与我的第一个示例相同的功能,但使用 Pandas 功能?

回答by IanS

Try:

尝试:

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

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

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

Edit by J.A.Cado

JACado 编辑

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

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

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