9
votes

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?

1
this answer might be useful for you stackoverflow.com/questions/17812978/… - Leukonoe
Leukonoe, tnx, but I have been unclear. Please read the question again, I have edited it. also, I just want to add, making one large dataframe out of the two is probably not an option. Both dataframes have unique x and y coordinates. - J.A.Cado
then perhaps this one will be usefull (used by @IanS as well stackoverflow.com/questions/13872533/… - Leukonoe
Wow, I rewrote the same code to the letter! You should mark the question as duplicate. - IanS

1 Answers

17
votes

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