-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_time_series_visualizer.py
75 lines (61 loc) · 2.95 KB
/
script_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
# Import modules
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', parse_dates = True, index_col = 'date', infer_datetime_format = True)
# Clean the data by filtering out days when the page views were in the top 2.5% of the dataset or bottom 2.5% of the dataset
df = df[
(df['value'] > df['value'].quantile(0.025)) &
(df['value'] < df['value'].quantile(0.975))]
# draw_line_plot() that creates a line chart of page views over time
def draw_line_plot():
# Draw line plot
fig, ax = plt.subplots(figsize = (25, 8))
plt.plot(df.index.values, df.value, color = 'red')
ax.set_title('Daily freeCodeCamp Forum Page Views 5/2016-12/2019')
ax.set_ylabel('Page Views')
ax.set_xlabel('Date')
# Save image and return fig
fig.savefig('line_plot.png')
return fig
# draw_bar_plot() that creates a bar plot of average page views by month over the years
def draw_bar_plot():
# Copy and modify data for monthly bar plot
df['month'] = pd.DatetimeIndex(df.index.values).month_name()
df['year'] = pd.DatetimeIndex(df.index.values).year
df_bar = df.groupby(['year', 'month'], as_index = False, sort = False)['value'].mean()
# Draw bar plot
Months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
fig, ax = plt.subplots(figsize = (12, 12))
sns.barplot(data = df_bar, x = 'year', y = 'value', hue = 'month', hue_order = Months, palette = 'husl')
plt.legend(loc = 'upper left', title = 'Months')
ax.set_ylabel('Average Page Views')
ax.set_xlabel('Years')
# Save image and return fig
fig.savefig('bar_plot.png')
return fig
# draw_box_plot() that creates two sets of boxplots. The first show the distribution of page views over each year.
# The second show the distribution of page views over the months.
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)
Months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
fig, ax = plt.subplots(ncols = 2, figsize = (20, 10))
sns.boxplot(data = df_box, x = 'year', y = 'value', ax = ax[0])
ax[0].set_title('Year-wise Box Plot (Trend)')
ax[0].set_ylabel('Page Views')
ax[0].set_xlabel('Year')
sns.boxplot(data = df_box, x = 'month', y = 'value', order = Months, ax = ax[1])
ax[1].set_title('Month-wise Box Plot (Seasonality)')
ax[1].set_ylabel('Page Views')
ax[1].set_xlabel('Month')
# Save image and return fig
fig.savefig('box_plot.png')
return fig