-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_series_visualizer.py
100 lines (85 loc) · 3.22 KB
/
time_series_visualizer.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
# Import data (Make sure to parse dates. Consider setting index column to 'date'.)
try:
df = pd.read_csv("fcc-forum-pageviews.csv", parse_dates=["date"], index_col="date")
except FileNotFoundError:
print("Error: Data file not found!")
# Clean data
df = df[(df.value > df.value.quantile(0.025))
& (df.value < df.value.quantile(0.975))]
def draw_line_plot():
# Draw line plot
fig, ax = plt.subplots(figsize=(16, 6))
ax.plot(df.index,
df["value"],
color='red',
linewidth=1)
ax.set_title('Daily freeCodeCamp Forum Page Views 5/2016-12/2019')
ax.set_xlabel('Date')
ax.set_ylabel('Page Views')
# Save image and return fig (don't change this part)
fig.savefig('line_plot.png')
return fig
def draw_bar_plot():
# Copy and modify data for monthly bar plot
df_bar = df.copy()
df_bar['year'] = df_bar.index.year
df_bar['month'] = df_bar.index.month
fig, ax = plt.subplots(figsize=(10, 9))
df_bar.groupby(['year', 'month'])['value'].mean().unstack().plot(kind='bar', ax=ax)
ax.set_xlabel('Years')
ax.set_ylabel('Average Page Views')
ax.set_title('Average Page Views per Year')
ax.legend(title='Months', labels=['January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'])
fig.tight_layout()
# Save image and return fig (don't change this part)
fig.savefig('bar_plot.png')
return fig
def draw_box_plot():
# Prepare data for box plots (this part is done!)
df_box = df.copy()
df_box.reset_index(inplace=True)
df_box['year'] = [d.year for d in df_box.date]
df_box['month'] = [d.strftime('%b') for d in df_box.date]
# Draw box plots (using Seaborn)
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(19, 10))
sns.boxplot(x=df_box['year'],
y=df_box['value'],
ax=axes[0])
axes[0].set_title('Year-wise Box Plot (Trend)')
axes[0].set_xlabel('Year')
axes[0].set_ylabel('Page Views')
sns.boxplot(x=df_box['month'], y=df_box['value'], ax=axes[1],
order=['Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'])
axes[1].set_title('Month-wise Box Plot (Seasonality)')
axes[1].set_xlabel('Month')
axes[1].set_ylabel('Page Views')
# Save image and return fig (don't change this part)
fig.savefig('box_plot.png')
return fig