12

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?

J.A.Cado
  • 655
  • 5
  • 13
  • 23
  • this answer might be useful for you http://stackoverflow.com/questions/17812978/how-to-plot-two-columns-of-a-pandas-data-frame-using-points – Leukonoe May 24 '16 at 12:46

1 Answers1

19

To plot all columns against the index as line plots.

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

A signal pandas.DataFrame.plot (not subplots=True) returns a matplotlib.axes.Axes, which you can then pass to the second dataframe.

To plot specific columns as x and y. Specifying x and y is required for scatter plots (kind='scatter').

ax = df1.plot(x='Lat', y='Lon', figsize=(8, 8))
df2.plot(ax=ax, x='Lat', y='Lon')
Trenton McKinney
  • 43,885
  • 25
  • 111
  • 113
IanS
  • 14,753
  • 9
  • 56
  • 81