I am having an issue with the Page View Time Series Visualizer. The next error is displaying after running my code.
======================================================================
FAIL: test_bar_plot_number_of_bars (test_module.BarPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/workspace/boilerplate-page-view-time-series-visualizer/test_module.py", line 63, in test_bar_plot_number_of_bars
self.assertEqual(actual, expected, "Expected a different number of bars in bar chart.")
AssertionError: 57 != 49 : Expected a different number of bars in bar chart.
----------------------------------------------------------------------
Ran 11 tests in 5.283s
FAILED (failures=1)
#This is my code:
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
from pandas.plotting import register_matplotlib_converters
from matplotlib import dates as mpl_dates
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=0)
df.index=pd.to_datetime(df.index)
# Clean data
df = df[(df['value']>=df['value'].quantile(0.025))&(df['value']<=df['value'].quantile(0.975))]
#df[df['value']<=df['value'].quantile(0.025)].index)
def draw_line_plot():
# Draw line plot
plt.figure(figsize=(10,6))
fig=sns.lineplot(data=df, x=df.index.values, y='value')
plt.title('Daily freeCodeCamp Forum Page Views 5/2016-12/2019')
plt.xlabel("Date")
plt.ylabel("Page Views")
fig=fig.figure
# 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['Year']=pd.DatetimeIndex(df_bar.index).year
df_bar['Month']=pd.DatetimeIndex(df_bar.index).month_name()
df_bar=df_bar.groupby(['Year','Month']).mean()
# Draw bar plot
plt.figure(figsize=(9,6))
fig=sns.barplot(
data=df_bar, x='Year', y='value', hue='Month'
,palette='bright',hue_order=['January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October',
'November', 'December']
)
fig.legend(title='Month')
fig.set_ylabel('Average Page Views')
fig.set_xlabel('Years')
sns.move_legend(fig, "upper left",borderaxespad=0,bbox_to_anchor=(0.01, 0.98))
fig=fig.figure
# 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)
fig, axes=plt.subplots(nrows=1,ncols=2,figsize=(11,6))
axes[0].set_xlabel('Year')
axes[0].set_ylabel('Page Views')
axes[0].set_title('Year-wise Box Plot (Trend)')
sns.boxplot(data=df_box,x='year', y='value', palette='bright',ax=axes[0])
axes[1].set_xlabel('Month')
axes[1].set_ylabel('Page Views')
axes[1].set_title('Month-wise Box Plot (Seasonality)')
sns.boxplot(data=df_box,x='month', y='value', palette='bright',order=['Jan','Feb','Mar','Apr','May',
'Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
ax=axes[1])
plt.tight_layout(pad=2)
fig=fig.figure
# Save image and return fig (don't change this part)
fig.savefig('box_plot.png')
return fig
And the bar plot image that my code generates is this one:
Still I can’t find where is my error. Many thanks in advance to the person who can help me.