forked from mscharth/forecasting
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplotting.py
78 lines (69 loc) · 3.24 KB
/
plotting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
""" Plotting methods for temp series """
__author__ = "Marcel Scharth"
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
def plot_components_x13(results, label=''):
colours=['#D62728', '#FF7F0E', '#2CA02C', '#1F77B4']
fig, ax = plt.subplots(2,2, figsize=(12,8))
ax[0,0].plot(results.observed, color=colours[0], alpha=0.95)
ax[0,0].set(ylabel=label, title='Observed')
ax[0,1].plot(results.trend, color=colours[1], alpha=0.95)
ax[0,1].set(title='Trend')
ax[1,0].plot(results.observed/results.seasadj, color=colours[2], alpha=0.95)
ax[1,0].set(ylabel=label, title='Seasonal')
ax[1,1].plot(results.irregular, color=colours[3], alpha=0.95)
ax[1,1].set(title='Irregular')
sns.despine()
fig.suptitle('Time series decomposition (X-13 ARIMA-SEATS)', fontsize=13.5)
fig.tight_layout()
fig.subplots_adjust(top=0.9)
return fig, ax
def plot_components_basic(results, label=''):
colours=['#D62728', '#FF7F0E', '#2CA02C', '#1F77B4']
fig, ax = plt.subplots(2,2, figsize=(12,8))
ax[0,0].plot(results.observed, color=colours[0], alpha=0.95)
ax[0,0].set(ylabel=label, title='Observed')
ax[0,1].plot(results.trend, color=colours[1], alpha=0.95)
ax[0,1].set(title='Trend')
ax[1,0].plot(results.seasonal, color=colours[2], alpha=0.95)
ax[1,0].set(ylabel=label, title='Seasonal')
ax[1,1].plot(results.resid, color=colours[3], alpha=0.95)
ax[1,1].set(title='Irregular')
sns.despine()
fig.suptitle('Time series decomposition (X-13 ARIMA-SEATS)', fontsize=13.5)
fig.tight_layout()
fig.subplots_adjust(top=0.9)
return fig, ax
def fanchart(y, forecast, intv1, intv2, intv3):
assert type(y)==pd.core.series.Series, 'The time series must be a pandas series'
assert type(forecast)==pd.core.series.Series, 'The forecast must be a pandas series'
last=y.iloc[-1:]
extended=last.append(forecast)
with sns.axes_style('ticks'):
fig, ax= plt.subplots(figsize=(8,5))
y.plot(color='#D62728')
extended.plot(color='black', alpha=0.4, label='Point forecast')
ax.fill_between(extended.index, last.append(intv3.iloc[:,0]), last.append(intv3.iloc[:,1]), facecolor='#FAB8A4', lw=0)
ax.fill_between(extended.index, last.append(intv2.iloc[:,0]), last.append(intv2.iloc[:,1]), facecolor='#F58671', lw=0)
ax.fill_between(extended.index, last.append(intv1.iloc[:,0]), last.append(intv1.iloc[:,1]), facecolor='#F15749', lw=0)
hold = ax.get_ylim()
ax.fill_betweenx(ax.get_ylim(), extended.index[0], extended.index[-1], facecolor='grey', alpha=0.15)
ax.set_ylim(hold)
return fig, ax
def histogram(series):
fig, ax= plt.subplots(figsize=(8,5))
sns.distplot(series, ax=ax, hist_kws={'alpha': 0.8, 'edgecolor':'black', 'color': '#1F77B4'},
kde_kws={'color': 'black', 'alpha': 0.7})
sns.despine()
return fig, ax
def qq_plot(residuals):
fig, ax = plt.subplots(figsize=(8,5))
pp = sm.ProbPlot(residuals, fit=True)
qq = pp.qqplot(color='#1F77B4', alpha=0.8, ax=ax)
a=ax.get_xlim()[0]
b=ax.get_xlim()[1]
ax.plot([a,b],[a,b], color='black', alpha=0.6)
ax.set_xlim(a,b)
ax.set_title('Normal Q-Q plot for the residuals', fontsize=12)
return fig, ax