My code works so far and the graphs that are generated look exactly like the given ones but when I run the test module I still fail one of the tests for the bar plot.
In my code I have 44 unique combinations for years and months plus the four months for which there is no data in 2016 and for those a value of 0 is assumed to display it correctly => 48 bars.
The following error is displayed in the terminal:
AssertionError: 57 != 49 : Expected a different number of bars in bar chart.
Where do the 57 bars come from and why are 49 bars expected if we consider 4 years = 48 months?
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=['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=(10, 5))
sns.lineplot(data=df, x=df.index, y='value', ax=ax, color='darkred')
ax.set_title('Daily freeCodeCamp Forum Page Views 5/2016-12/2019')
ax.set_xlabel('Date')
ax.set_ylabel('Page Views')
# 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.reset_index(inplace=True)
df_bar['year_month'] = df_bar['date'].dt.to_period('M')
df_bar = df_bar.groupby('year_month')['value'].mean().reset_index()
df_bar['year'] = df_bar['year_month'].dt.year
df_bar['month'] = df_bar['year_month'].dt.strftime('%B')
months_order = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
df_bar['month'] = pd.Categorical(df_bar['month'], categories=months_order, ordered=True)
# Draw bar plot
fig, ax = plt.subplots(figsize=(10, 8))
sns.barplot(data=df_bar, x='year', y='value', ax=ax, hue='month', palette='deep')
ax.set_xlabel('Years')
ax.set_ylabel('Average Page Views')
ax.legend(title='Months')
# 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]
months_order = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
df_box['month'] = pd.Categorical(df_box['month'], categories=months_order, ordered=True)
# Draw box plots (using Seaborn)
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
sns.boxplot(x='year', y='value', data=df_box, fliersize=1, palette='deep', ax=axes[0])
axes[0].set_title('Year-wise Box Plot (Trend)')
axes[0].set_xlabel('Year')
axes[0].set_ylabel('Page Views')
sns.boxplot(x='month', y='value', data=df_box, fliersize=1, palette='deep', 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