In [1]:
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

In [2]:
#create a sample dataframe
idx = pd.date_range('1/1/2001', periods=(1095), freq='d', name='date')
dat = np.random.randint(0, 25, size=1095)
df = pd.DataFrame(dat, index=idx, columns=['temp'])
df['year'] = df.index.year
df['day'] = df.index.dayofyear

In [3]:
print(df.shape)
df.head()


(1095, 3)
Out[3]:
temp year day
date
2001-01-01 21 2001 1
2001-01-02 11 2001 2
2001-01-03 9 2001 3
2001-01-04 3 2001 4
2001-01-05 15 2001 5

In [4]:
df.plot()


Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x17294b583c8>

In [5]:
# gives a graph like df.plot() but in three different colors, one per year
a = df.iloc[:365,0]
b = df.iloc[365:730,0]
c = df.iloc[730:1095,0]
fig, ax = plt.subplots()
for year in (a,b,c):
    ax.plot(year)
plt.show()



In [6]:
df.groupby(['year']).head(3)


Out[6]:
temp year day
date
2001-01-01 21 2001 1
2001-01-02 11 2001 2
2001-01-03 9 2001 3
2002-01-01 18 2002 1
2002-01-02 9 2002 2
2002-01-03 5 2002 3
2003-01-01 4 2003 1
2003-01-02 8 2003 2
2003-01-03 19 2003 3

In [7]:
print(df.shape)


(1095, 3)

In [8]:
fig, ax = plt.subplots()
for key, grp in df.groupby(['year']): 
    ax.plot(grp['day'], grp['temp'], alpha=0.3)
plt.show()



In [10]:
df.T


Out[10]:
date 2001-01-01 00:00:00 2001-01-02 00:00:00 2001-01-03 00:00:00 2001-01-04 00:00:00 2001-01-05 00:00:00 2001-01-06 00:00:00 2001-01-07 00:00:00 2001-01-08 00:00:00 2001-01-09 00:00:00 2001-01-10 00:00:00 ... 2003-12-22 00:00:00 2003-12-23 00:00:00 2003-12-24 00:00:00 2003-12-25 00:00:00 2003-12-26 00:00:00 2003-12-27 00:00:00 2003-12-28 00:00:00 2003-12-29 00:00:00 2003-12-30 00:00:00 2003-12-31 00:00:00
temp 16 1 13 8 4 17 14 12 24 20 ... 2 4 24 22 17 5 20 19 24 0
day# 1 2 3 4 5 6 7 8 9 10 ... 356 357 358 359 360 361 362 363 364 365

2 rows × 1095 columns


In [13]:
#https://stackoverflow.com/questions/38197964/pandas-how-to-plot-multiple-time-series-into-a-single-plot-from-a-single-datafr

import pandas as pd
import statsmodels.api as sm
import matplotlib.pylab as plt
from pandas.tools.plotting import andrews_curves

data = sm.datasets.get_rdataset('airquality').data
fig, (ax1, ax2) = plt.subplots(nrows = 2, ncols = 1)
data = data[data.columns.tolist()[3:]] # use only Temp, Month, Day

# Andrews' curves
andrews_curves(data, 'Month', ax=ax1)

# multiline plot with group by
for key, grp in data.groupby(['Month']): 
    ax2.plot(grp['Day'], grp['Temp'], label = "Temp in {0:02d}".format(key))
plt.legend(loc='best')    
plt.show()


C:\Program Files\Anaconda3\lib\site-packages\ipykernel_launcher.py:13: FutureWarning: 'pandas.tools.plotting.andrews_curves' is deprecated, import 'pandas.plotting.andrews_curves' instead.
  del sys.path[0]