Data Analysis with Python Projects - Page View Time Series Visualizer

Tell us what’s happening:

I finished coding my time series visualizer locally, ran it using Jupyter Notebook and it runs smoothly and I get the expected plots. However, when I test it get the following error: UnboundLocalError: cannot access local variable ‘boxprops’ where it is not associated with a value. I reckon there is a problem with my boxplots, but I could not figure out what the problem was.

Your code so far

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(1, 2, figsize=(20, 8))

    sns.boxplot(
        data=df_box,
        x='year',
        y='value',
        ax=axes[0],
        hue='year',
        flierprops=dict(marker='.', markersize=5),
        palette='bright',
        legend=False
    )
    sns.boxplot(
        data=df_box,
        x='month',
        y='value',
        ax=axes[1],
        hue='month',
        flierprops=dict(marker='.', markersize=5),
        palette='hls',
        legend=False,
        order=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    )

    axes[0].set_title('Year-wise Box Plot (Trend)')
    axes[0].set_xlabel('Year')
    axes[0].set_ylabel('Page Views')

    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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0

Challenge Information:

Data Analysis with Python Projects - Page View Time Series Visualizer

Not able to replicate your error.

Can you share your full code and versions of numpy and seaborn?

I used seaborn-0.13.2 and numpy-1.20.3

Also, does the code come from your code or from the fCC test suite?

(Do you still get the error if you comment out this line:

# Run unit tests automatically
#main(module='test_module', exit=False)

)

I am using seaborn-0.13.2 as well, numpy- 1.26.2, however I am not importing numpy as there is no need for it.
My 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', index_col='date', parse_dates=['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=(15, 5))
    ax.plot(df.index, df.values, color='darkred')
    ax.set_xlabel('Date')
    ax.set_ylabel('Page Views')
    ax.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

def draw_bar_plot():
    # Copy and modify data for monthly bar plot
    df_bar = df.copy()
    df_bar = df_bar.groupby([df_bar.index.year, df_bar.index.month]).mean().unstack(fill_value=0)
    df_bar.columns = [
        'January',
        'February',
        'March',
        'April',
        'May',
        'June',
        'July',
        'August',
        'September',
        'October',
        'November',
        'December'
    ]

    # Draw bar plot
    fig, ax = plt.subplots(figsize=(7, 8))

    df_bar.plot(kind='bar', ax=ax)

    ax.set_xlabel('Years')
    ax.set_ylabel('Average Page Views')
    ax.legend(title='Months', loc='best')

    # 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(1, 2, figsize=(20, 8))

    sns.boxplot(
        data=df_box,
        x='year',
        y='value',
        ax=axes[0],
        hue='year',
        flierprops=dict(marker='.', markersize=5),
        palette='bright',
        legend=False
    )
    sns.boxplot(
        data=df_box,
        x='month',
        y='value',
        ax=axes[1],
        hue='month',
        flierprops=dict(marker='.', markersize=5),
        palette='hls',
        legend=False,
        order=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    )

    axes[0].set_title('Year-wise Box Plot (Trend)')
    axes[0].set_xlabel('Year')
    axes[0].set_ylabel('Page Views')

    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

As for the last question, I still get the error when I comment out the line.

Pandas does use numpy under the hood, so you will be using it. Can you try downgrading numpy to the same version? I did that to fix a different error though, I was never able to replicate yours.

It doesn’t sound related to your code tbh (you don’t have a variable with that name). And it’s not related to the tests. It sounds like something related to the environment maybe.

Can you try duplicating your gitpod workspace? Maybe something about that workspace is corrupted.

Again, I created a new gitpod workspace and imported your code and was not able to get the same error.

I think you might be right about the numpy version causing the error. So, here is what I did: I created a new Gitpod workspace and imported my code. I got some weird error that I assumed was related to either the numpy or seaborn version in the workspace environment. Seaborn version was 0.9.0, so I upgraded it and now it ran smoothly. I did not try downgrading the numpy version locally though. But I think it is good enough for me that I got it to run in the gitpod workspace.

That said, I really appreciate your help, I was going nuts trying to solve the issue. I thought it might be directly related to the libraries, but did not know how to tackle the problem by myself, and couldn’t find anything online. Thank you very much for your time.

how do you downgrade your numpy version

You can open a new topic for your question, but I think it would be faster to just search for this answer yourself.