I have a temperature file with many years temperature records, in a format as below:
2012-04-12,16:13:09,20.6 2012-04-12,17:13:09,20.9 2012-04-12,18:13:09,20.6 2007-05-12,19:13:09,5.4 2007-05-12,20:13:09,20.6 2007-05-12,20:13:09,20.6 2005-08-11,11:13:09,20.6 2005-08-11,11:13:09,17.5 2005-08-13,07:13:09,20.6 2006-04-13,01:13:09,20.6
Every year has different numbers, time of the records, so the pandas datetimeindices are all different.
I want to plot the different year’s data in the same figure for comparing . The X-axis is Jan to Dec, the Y-axis is temperature. How should I go about doing this?
(Assuming you have this in dataframe, with datetime index already)
In : df Out: value datetime 2010-01-01 1 2010-02-01 1 2009-01-01 1 # create additional month and year columns for convenience df['Month'] = map(lambda x: x.month, df.index) df['Year'] = map(lambda x: x.year, df.index) In : df.groupby(['Month','Year']).mean().unstack() Out: value Year 2009 2010 Month 1 1 1 2 NaN 1
Now it’s easy to plot (each year as a separate line):
ax = df1.plot() df2.plot(ax=ax)
If you a running Jupyter/Ipython notebook and having problems using;
ax = df1.plot()
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.
To do this for multiple dataframes, you can do a for loop over them:
fig = plt.figure(num=None, figsize=(10, 8)) ax = dict_of_dfs['FOO'].column.plot() for BAR in dict_of_dfs.keys(): if BAR == 'FOO': pass else: dict_of_dfs[BAR].column.plot(ax=ax)
Just to enhance @adivis12 answer, you don’t need to do the
if statement. Put it like this:
fig, ax = plt.subplots() for BAR in dict_of_dfs.keys(): dict_of_dfs[BAR].plot(ax=ax)