-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_series_visualizer.py
87 lines (66 loc) · 2.85 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
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")
df = df.set_index("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=(10, 4))
x = df.index
y = df["value"]
plt.plot(x, y, color="red")
plt.xlabel("Date")
plt.ylabel("Page Views")
plt.title("Daily freeCodeCamp Forum Page Views 5/2016-12/2019")
plt.xticks(x[::(len(x)//7)])
# 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.index = pd.to_datetime(df_bar.index)
df_bar["Years"] = df_bar.index.year
df_bar["Months"] = df_bar.index.month_name()
df_bar = pd.DataFrame(df_bar.groupby(["Years", "Months"], sort=False)["value"].mean().round().astype(int))
df_bar = df_bar.rename(columns={"value" : "Average Page Views"})
cat = {"Years" : [2016, 2016, 2016, 2016], "Months": ["January", "February", "March", "April"], "Avarage Page Views" : [0, 0, 0, 0]}
df_bar = df_bar.reset_index()
df_bar = pd.concat([pd.DataFrame(cat), df_bar])
df_bar_pivot = df_bar.pivot(index="Years", columns="Months", values="Average Page Views")
df_bar_pivot = df_bar_pivot.reindex(df_bar["Months"][:12], axis=1)
fig = df_bar_pivot.plot(kind="bar", figsize=(10, 5))
plt.xlabel("Years")
plt.ylabel("Average Page Views")
fig = fig.figure
# Draw bar plot
# 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.index = pd.to_datetime(df_box.index)
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]
df_box["num"] = df_box["date"].dt.month
df_box = df_box.sort_values("num")
# Draw box plots (using Seaborn)
fig, axes = plt.subplots(1, 2, figsize=(15, 7))
axes[0] = 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')
axes[1] = sns.boxplot(x=df_box["month"], y=df_box["value"], ax=axes[1])
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