Data Analysis with Python Projects - Page View Time Series Visualizer

Hi All! Thanks in advance for your help!

I’ve been pulling my hair out on this issue.

I have one test failing on the Page View Time Series Visualizer. (Error listed below) The test is expecting 49 bars but mine is reporting 57. If I remove the legend, then it report 45.

This appears to be the same exact issue in another post, but it doesn’t appear that a resolution was ever found. I’d post the link but I can only post one link in here and I figured my repl was more important.

Here is the link to my repl

Error Received

======================================================================
FAIL: test_bar_plot_number_of_bars (test_module.BarPlotTestCase)

Traceback (most recent call last):
File “/home/runner/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.


Your code so far

def draw_bar_plot():
# Copy and modify data for monthly bar plot
df_bar = df.copy()
df_bar[‘year’] = df_bar.index.year
df_bar[‘month’] = df_bar.index.month_name()
df_bar = df_bar.groupby([‘year’, ‘month’]).mean().reset_index()
month_order = [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’]
df_bar[‘month’] = pd.Categorical(df_bar[‘month’], categories=month_order, ordered=True)
df_bar = df_bar.sort_values(by=[‘year’, ‘month’])

# Draw bar plot
fig, ax = plt.subplots(figsize=(10,5))
sns.barplot(data=df_bar, x='year', y='value', hue='month', palette='Set1', hue_order=month_order, legend=True)
ax.legend(title='Months')
ax.set_xlabel('Years')
ax.set_ylabel('Average Page Views')

# Save image and return fig (don't change this part)
fig.savefig('bar_plot.png')
return fig

Your browser information:

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

Challenge Information:

Data Analysis with Python Projects - Page View Time Series Visualizer

This is a complicated one. When I run your code I get 7 Test fails, not 1.

3 for the bar chart and 4 for the boxplot. Is that accurate?

For the Bar Chart:

df_bar = df_bar.groupby(['year', 'month']).mean().reset_index()

You need to use .unstack() so that your df has this format:

month        1         2        3        4        5        6        7   \
year                                                                     
2016        NaN       NaN      NaN      NaN  19432.4  21875.1  24109.7   
2017    32785.2   31113.1  29369.1  30878.7  34244.3  43577.5  65806.8   
2018    58580.1   65679.0  62693.8  62350.8  56562.9  70117.0  63591.1   
2019   102056.5  105968.4  91214.5  89368.4  91439.9  90435.6  97236.6   

month_order = ['January', 'February', 'Ma

Try using this to correct the column names from numbers to names:
df_bar.columns = ['January',

sns.barplot(data=df_bar, x='year', y='value', hue='month', palette='Set1',

Don’t use seaborn here since it isn’t asked for until the boxplot.

I have the exact same problem.
Using the following code, I was able to make the plot without seaborn.
But still then I have 45 bars ( instead of the required 49).

def draw_bar_plot():
df_bar = df.copy()
df_bar['month'] = df_bar.index.strftime('%B')
df_bar['year'] = df_bar.index.year
df_bar = df_bar.groupby(['year', 'month']).mean().reset_index()
month_order = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
df_bar['month'] = pd.Categorical(df_bar['month'], categories=month_order, ordered=True)

# Draw bar plot
width = 0.25
years = df_bar['year'].unique().tolist()
x = np.arange(1, 1 + len(years) * 5, step=5)
multiplier= 0
fig,ax = plt.subplots()

for thismonth in month_order:
    offset = width* multiplier
    y =df_bar.loc[df_bar['month']==thismonth,'value']
    if len(y)<len(years):
        xnew = x[1:]
        ax.bar(xnew+offset,list(y),width,label=thismonth)
    else:
        ax.bar(x+offset,y,width,label=thismonth)
    multiplier += 1

ax.set_xticks([2.5, 7.5, 12.5, 17.5])
ax.set_xticklabels(years,rotation=90)

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

# Save image and return fig (don't change this part)
fig.savefig('bar_plot.png')
return fig

Please open a new topic, thanks!