J.A.Cado J.A.Cado - 6 months ago 38
Python Question

Plotting multiple dataframes using pandas functionality

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?

Answer

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.

Edit by J.A.Cado

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')