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()
Out[3]:
In [4]:
df.plot()
Out[4]:
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]:
In [7]:
print(df.shape)
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]:
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()