Here’s the full code.
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')
# Clean data
df = df[(df['value'] > df['value'].quantile(0.025))
& (df['value'] <= df['value'].quantile(0.975))]
# Draw Line Plot
def draw_line_plot():
# Draw line plot
fig, axes = plt.subplots(figsize=(18, 6))
axes.plot(df, color='r')
axes.set_xlabel('Date')
axes.set_ylabel('Page Views')
axes.set_title("Daily freeCodeCamp Forum Page Views 5/2016-12/2019")
# Save image and return fig (don't change this part)
fig.savefig('line_plot.png')
return fig
# Draw Bar Plot
def draw_bar_plot():
# Copy and modify data for monthly bar plot
df_bar = df.copy()
df_bar.reset_index(inplace=True)
df_bar['year'] = [d.year for d in df_bar.date]
df_bar['month'] = [d.month for d in df_bar.date]
# Create monthly average plot
df_avg_bar = pd.DataFrame(df_bar.groupby(['year', 'month'])['value'].mean().round(decimals = 2))
df_avg_bar.reset_index(inplace = True)
# Month list for Legend
month_list = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
# Draw bar plot
# fig, ax = plt.subplots(figsize=(10, 15))
fig = plt.figure()
sns.barplot(data = df_avg_bar, x = 'year', y = 'value', hue = 'month')
plt.xlabel('Years')
plt.ylabel('Average Page Views')
plt.legend(month_list, loc='upper left')
# Save image and return fig (don't change this part)
fig.savefig('bar_plot.png')
return fig
# Draw Box Plot
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]
# Month List for ordering Seasonality Plot
month_list = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# Draw box plots (using Seaborn)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(18, 6))
# Yearly Box Plot
sns.boxplot(data=df_box, x = 'year', y = 'value', hue = 'year', ax = ax1)
ax1.set_title('Year-wise Box Plot (Trend)')
ax1.set_xlabel('Year')
ax1.set_ylabel('Page Views')
# Monthly Box Plot
sns.boxplot(data=df_box, x='month', y='value',
order = month_list, hue = 'month', ax = ax2)
ax2.set_title('Month-wise Box Plot (Seasonality)')
ax2.set_xlabel('Month')
ax2.set_ylabel('Page Views')
# Save image and return fig (don't change this part)
fig.savefig('box_plot.png')
return fig
Can someone explain what’s going on? This error isn’t very helpful.