Plot different DataFrames in the same figure Plot different DataFrames in the same figure pandas pandas

Plot different DataFrames in the same figure


Try:

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


If you a running Jupyter/Ipython notebook and having problems using;

ax = df1.plot()

df2.plot(ax=ax)

Run the command inside of the same cell!! It wont, for some reason, work when they are separated into sequential cells. For me at least.


Although Chang's answer explains how to plot multiple times on the same figure, in this case you might be better off in this case using a groupby and unstacking:

(Assuming you have this in dataframe, with datetime index already)

In [1]: dfOut[1]:            value  datetime                         2010-01-01      1  2010-02-01      1  2009-01-01      1  # create additional month and year columns for conveniencedf['Month'] = map(lambda x: x.month, df.index)df['Year'] = map(lambda x: x.year, df.index)    In [5]: df.groupby(['Month','Year']).mean().unstack()Out[5]:       value      Year    2009  2010Month             1          1     12        NaN     1

Now it's easy to plot (each year as a separate line):

df.groupby(['Month','Year']).mean().unstack().plot()