-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_series_visualizer.py
More file actions
84 lines (69 loc) · 2.84 KB
/
time_series_visualizer.py
File metadata and controls
84 lines (69 loc) · 2.84 KB
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
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'.)
df = pd.read_csv('fcc-forum-pageviews.csv', index_col='date', parse_dates=['date'])
# Clean data - remove top 2.5% and bottom 2.5% data.
df = df[(df["value"] >= df["value"].quantile(0.025)) & (df["value"] <= df["value"].quantile(0.975))]
months_dict = {
"01": "January",
"02": "February",
"03": "March",
"04": "April",
"05": "May",
"06": "June",
"07": "July",
"08": "August",
"09": "September",
"10": "October",
"11": "November",
"12": "December"
}
months_list = months_dict.values()
def draw_line_plot():
# Draw line plot
fig = plt.figure(figsize=(16, 85))
plt.plot(df, 'r')
plt.title("Daily freeCodeCamp Forum Page Views 5/2016-12/2019")
plt.xlabel("Date")
plt.ylabel("Page Views")
# Save image and return fig (don't change this part)
fig.savefig('line_plot.png')
return fig
def draw_bar_plot():
bar_list = [[str(df.index[i]).split("-")[0], months_dict[str(df.index[i]).split("-")[1]], df["value"][i]] for i in range(len(df))]
# Copy and modify data for monthly bar plot
# Draw bar plot
fig = plt.figure(figsize = (12, 12))
df_bar = pd.DataFrame(bar_list, columns = ["Years", "Months", "Average Page Views"])
sns.barplot(x="Years", y="Average Page Views", hue="Months", hue_order=months_list, data=df_bar, ci=0, saturation=1)
# 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)
# separate the months so they can be ordered
df_box['month_num'] = df_box['date'].dt.month
df_box = df_box.sort_values('month_num')
# create both of the plots, grouped by particular date feature
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(15,10))
axes[0] = sns.boxplot(x=df_box['year'], y=df_box['value'], ax=axes[0])
axes[1] = sns.boxplot(x=df_box['month'], y=df_box['value'], ax=axes[1])
# set labels for the year
axes[0].set_title("Year-wise Box Plot (Trend)")
axes[0].set_xlabel("Year")
axes[0].set_ylabel("Page Views")
# set labels for the month
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