As requested I opened a new topic for this issue.
In test_bar_plot_number_of_bars I get the following failure message.
FAIL: test_bar_plot_number_of_bars (test_module.BarPlotTestCase)
Traceback (most recent call last):
File "/Users/martinjendryka/Documents/Training/freecodecamp/freecodecamp_projects/Data Analysis with Python/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: 45 != 49 : Expected a different number of bars in bar chart.
I dont know how to change my code to get the requested number of bars (using seaborn gives me 57 bars). The test_module counts the output of ax.get_children() belonging to rectangles. To me 45 makes sense since 44 rectangles correspond to each bar and one rectangle to the legend.
Minimal example:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
file_name = 'fcc-forum-pageviews.csv'
df = pd.read_csv(file_name)
df.index = pd.date_range(start="2016-05-09",end = "2019-12-03")
df= df.drop("date",axis=1)
# Clean data
df = df.loc[(df['value']>df['value'].quantile(0.025)) & (df['value' <df['value'].quantile(0.975)),:]
def draw_bar_plot():
# Copy and modify data for monthly 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