-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_series_visualizer.py
83 lines (67 loc) · 2.35 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
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
df = pd.read_csv('fcc-forum-pageviews.csv', parse_dates=['date'], index_col='date')
# 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=(12, 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')
ax.grid(True)
# Save image and return fig
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
df_bar_grouped = df_bar.groupby(['year', 'month'])['value'].mean().unstack()
# Draw bar plot
fig = df_bar_grouped.plot.bar(
figsize=(12, 6), legend=True, ylabel="Average Page Views"
).get_figure()
plt.xlabel('Years')
plt.ylabel('Average Page Views')
plt.legend(
title='Months',
labels=[
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
]
)
plt.tight_layout()
# Save image and return fig
fig.savefig('bar_plot.png')
return fig
def draw_box_plot():
# Prepare data for box plots
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]
month_order = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
]
# Draw box plots (using Seaborn)
fig, axes = plt.subplots(1, 2, figsize=(15, 6))
sns.boxplot(
x='year', y='value', data=df_box, ax=axes[0]
).set(title='Year-wise Box Plot (Trend)', xlabel='Year', ylabel='Page Views')
sns.boxplot(
x='month', y='value', data=df_box, ax=axes[1], order=month_order
).set(title='Month-wise Box Plot (Seasonality)', xlabel='Month', ylabel='Page Views')
# Save image and return fig
fig.savefig('box_plot.png')
return fig