Page View Time Series Visualizer

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.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

I’m not able to replicate your error (I got different errors), I tried on replit and gitpod. Are you able to link me to a running version of this?

Chart looks pretty good by eye. The instructions do not ask you to use seaborn to render the bar plot, not sure if that would cause this error or not.

1 Like

I also get this exact error with my project. This is also the only test it is failing.

Like you, I also used Seaborn to plot my bar plot.

I had a look into how the test module is checking the bar plot, and it’s counting the number of rectangles in ax.get_children(). When I print my list, I get this:

Rectangle(xy=(0.6, 0), width=0.0666667, height=32785.2, angle=0)
Rectangle(xy=(1.6, 0), width=0.0666667, height=58580.1, angle=0)
Rectangle(xy=(2.6, 0), width=0.0666667, height=102057, angle=0)
Rectangle(xy=(0.666667, 0), width=0.0666667, height=31113.1, angle=0)
Rectangle(xy=(1.66667, 0), width=0.0666667, height=65679, angle=0)
Rectangle(xy=(2.66667, 0), width=0.0666667, height=105968, angle=0)
Rectangle(xy=(0.733333, 0), width=0.0666667, height=29369.1, angle=0)
Rectangle(xy=(1.73333, 0), width=0.0666667, height=62693.8, angle=0)
Rectangle(xy=(2.73333, 0), width=0.0666667, height=91214.5, angle=0)
Rectangle(xy=(0.8, 0), width=0.0666667, height=30878.7, angle=0)
Rectangle(xy=(1.8, 0), width=0.0666667, height=62350.8, angle=0)
Rectangle(xy=(2.8, 0), width=0.0666667, height=89368.4, angle=0)
Rectangle(xy=(-0.133333, 0), width=0.0666667, height=19432.4, angle=0)
Rectangle(xy=(0.866667, 0), width=0.0666667, height=34244.3, angle=0)
Rectangle(xy=(1.86667, 0), width=0.0666667, height=56562.9, angle=0)
Rectangle(xy=(2.86667, 0), width=0.0666667, height=91439.9, angle=0)
Rectangle(xy=(-0.0666667, 0), width=0.0666667, height=21875.1, angle=0)
Rectangle(xy=(0.933333, 0), width=0.0666667, height=43577.5, angle=0)
Rectangle(xy=(1.93333, 0), width=0.0666667, height=70117, angle=0)
Rectangle(xy=(2.93333, 0), width=0.0666667, height=90435.6, angle=0)
Rectangle(xy=(-6.93889e-18, 0), width=0.0666667, height=24109.7, angle=0)
Rectangle(xy=(1, 0), width=0.0666667, height=65806.8, angle=0)
Rectangle(xy=(2, 0), width=0.0666667, height=63591.1, angle=0)
Rectangle(xy=(3, 0), width=0.0666667, height=97236.6, angle=0)
Rectangle(xy=(0.0666667, 0), width=0.0666667, height=31049.2, angle=0)
Rectangle(xy=(1.06667, 0), width=0.0666667, height=47712.5, angle=0)
Rectangle(xy=(2.06667, 0), width=0.0666667, height=62831.6, angle=0)
Rectangle(xy=(3.06667, 0), width=0.0666667, height=102717, angle=0)
Rectangle(xy=(0.133333, 0), width=0.0666667, height=41476.9, angle=0)
Rectangle(xy=(1.13333, 0), width=0.0666667, height=47376.8, angle=0)
Rectangle(xy=(2.13333, 0), width=0.0666667, height=65941.7, angle=0)
Rectangle(xy=(3.13333, 0), width=0.0666667, height=97268.8, angle=0)
Rectangle(xy=(0.2, 0), width=0.0666667, height=27398.3, angle=0)
Rectangle(xy=(1.2, 0), width=0.0666667, height=47438.7, angle=0)
Rectangle(xy=(2.2, 0), width=0.0666667, height=111378, angle=0)
Rectangle(xy=(3.2, 0), width=0.0666667, height=122802, angle=0)
Rectangle(xy=(0.266667, 0), width=0.0666667, height=40448.6, angle=0)
Rectangle(xy=(1.26667, 0), width=0.0666667, height=57701.6, angle=0)
Rectangle(xy=(2.26667, 0), width=0.0666667, height=78688.3, angle=0)
Rectangle(xy=(3.26667, 0), width=0.0666667, height=143166, angle=0)
Rectangle(xy=(0.333333, 0), width=0.0666667, height=27832.4, angle=0)
Rectangle(xy=(1.33333, 0), width=0.0666667, height=48420.6, angle=0)
Rectangle(xy=(2.33333, 0), width=0.0666667, height=80047.5, angle=0)
Rectangle(xy=(3.33333, 0), width=0.0666667, height=150734, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Rectangle(xy=(0, 0), width=0, height=0, angle=0)
Spine
Spine
Spine
Spine
XAxis(103.75000000000001,58.29999999999999)
YAxis(103.75000000000001,58.29999999999999)
Text(0.5, 1.0, '')
Text(0.0, 1.0, '')
Text(1.0, 1.0, '')
Legend
Rectangle(xy=(0, 0), width=1, height=1, angle=0)

For reference, this is the bar plot I’ve generated:

I’m not sure what to make of the extra rectangles or even where they might have come from. I tried checking my data for extra months or years that could have popped up from somewhere, but there weren’t any. Perhaps it is something to do with using the Seaborn barplot function instead of a function from pandas or matplotlib?

I’ll re-iterate that the instructions only ask for seaborn in the next chart, so I’m not sure why you’re using it for this one?

Create a draw_bar_plot function that draws a bar chart

Create a draw_box_plot function that uses Seaborn

Generally you should follow exactly what the instructions say.

Thanks for the clarification about the instructions. I originally thought that it wouldn’t matter how I got the chart as it didn’t specify.

I changed my code to use the pandas plot method instead, and it passed the test!

It did involve more steps than using Seaborn as I had to manipulate the dataframe before plotting it. My previous code was relying on Seaborn to do a lot of the work for me. For example, Seaborn automatically aggregated the data and calculated means, and it was easier to re-order the data using it.

1 Like

Good to know! I wasn’t sure if that would make a difference or not

Maybe the other errors are due to it has to be upgraded the version of seaborn. Because I was having those too. I followed your advice and create the bar plot like this, fig = df_bar.plot(kind =“bar”, figsize=(9,6)).figure. However, I had to organize the data before that in order to have the same result by using the function .unstack()

1 Like