Data Analysis with Python Projects - columns won't work in gitpod despite working in jupyter notebook

Tell us what’s happening:

In my local jupyter notebook I am able to use an index as a column in the “sns.catplot()” function but I am not able to do so in gitpod despite identical code.

Note, This explanation of the issue will be be difficult to parse due to an inability to post multiple images. When I have the permission to do either, I will include the relevant images.

I was able to reformat the cleaned version of the dataframe to have seperate year and month columns and then I was able to regroup it in order to prepare for the bar chart using the code below:

time_group=cleaned_df.groupby(["year","month"])[["value"]].mean()

month_labels ={1:"January", 2:"February",3:"March",4:"April",5:"May",6:"June",7:"July",8:"August",9:"September",10:"October",11:"November",12:"December"}

time_group=time_group.replace({"month":month_labels})

time_group.index = time_group.index.set_levels([time_group.index.levels[0], pd.to_datetime(time_group.index.levels[1], format='%m').strftime('%B')], level=[0, 1])

In my local jupyter notebook the below code resulted in a barchart grouped by years and months:
g = sns.catplot(
    data=time_group, kind="bar",
    x="year", y="value", hue="month",
    errorbar="sd", palette="dark", alpha=.6, height=6
)
g.despine(left=True)
g.set_axis_labels("Years", "Average Page Views")
g.legend.set_title("Months")

However, it did not work in the below gitpod
https://gitpod.io#snapshot/d463851f-7a6d-4d45-bdf2-c185069e64fa

Could you please explain the discrepancy?

I’d prefer not to change the basic approach since I already have been able to demonstrate that it works on my local development enviroment so I don’t think there’s any issue with using the index as a column in this function.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 OPR/116.0.0.0

Challenge Information:

Data Analysis with Python Projects - Page View Time Series Visualizer

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 (').

1 Like

Thank you I appreciate it

Can you please provide your full code?

Provide any error messages or details? What goes wrong exactly?

If the above link didn’t work then here is a new one

https://gitpod.io#snapshot/0b808976-7914-47b1-8edd-8d44f8c6d6f1

Here’s a picture of the code in the gitpod and the error

Link isn’t working for me.

Can you paste the code here? Not as a screenshot.

could it be that you are using two different versions of the module?

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")
df["date"]=pd.to_datetime(df["date"])
# Clean data
cleaned_df=df[(df["value"]>=df["value"].quantile(0.025))&(df["value"]<=df["value"].quantile(0.975))]


def draw_line_plot():
    # Draw line plot
    plt.rcParams['figure.figsize'] = 10,8
    fig, ax = plt.subplots()
    x=cleaned_df["date"]
    y=cleaned_df["value"]
    ax.grid(True)
    ax.plot(x, y)
    ax.set(xlabel='date', ylabel='Page Views',
       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

cleaned_df["year"]=cleaned_df["date"].dt.year
cleaned_df["month"]=cleaned_df["date"].dt.month
cleaned_df=cleaned_df.sort_values(by = ["year","month"])

def draw_bar_plot():
    # Copy and modify data for monthly bar plot
    time_group=cleaned_df.groupby(["year","month"])[["value"]].mean()
    month_labels ={1:"January", 2:"February",3:"March",4:"April",5:"May",6:"June",7:"July",8:"August",9:"September",10:"October",11:"November",12:"December"}
    time_group=time_group.replace({"month":month_labels})
    time_group.index = time_group.index.set_levels([time_group.index.levels[0], pd.to_datetime(time_group.index.levels[1], format='%m').strftime('%B')], level=[0, 1])
    # Draw bar plot
    
    g = sns.catplot(
        x="year", y="value", hue="month",
        data=time_group, kind="bar",
        errorbar="sd", palette="dark", alpha=.6, height=6)
    g.despine(left=True)
    g.set_axis_labels("Years", "Average Page Views")
    g.legend.set_title("Months")
    g.savefig('bar_plot.png')
    return g
print(cleaned_df.head())
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)
    plt.rcParams['figure.figsize'] = 16,10
    f, axes = plt.subplots(1, 2)
    sns.boxplot(x="year",y="value" ,hue = "year", legend=False,
    data=cleaned_df, palette=sns.color_palette(),ax=axes[0]).set(xlabel ="Year", ylabel = "Page Views",title = "Year-wise Box Plot (Trend)")
    sns.boxplot(x="month",y="value", hue = "month", order =["January","February","March","April","May","June","July","August","September","October","November","December"],
                                                 legend=False, data=cleaned_df.replace({"month":month_labels}), palette=sns.color_palette(),ax=axes[1]).set(xlabel ="Month", ylabel = "Page Views", title = "Month-wise Box Plot (Seasonality)")
    fig.tight_layout()
    # Save image and return fig (don't change this part)
    fig.savefig('box_plot.png')
    return fig

Here you go

This is the snapshot of my code. Let me know if you are having further issues.

Haven’t had a chance to dig into this yet, but I suspect @ILM would be on the correct path, it’s probably a difference in the versions of matplotlib, pandas and/or seaborn that are installed on each system. That’s why the same code would behave differently.

The other problem is that your bar chart isn’t correct. Look at the example bar chart and you can see that the shape of the bars is quite different. It seems like the months are in a different order.

This seems like it would be part of the problem:

Create a draw_bar_plot function that draws a bar chart

g = sns.catplot

You are drawing a seaborn catplot. Don’t use seaborn here, and use a bar plot.