Tell us what’s happening:
It is about the unit test in this project. test_bar_plot_number_of_bars
The expected number of bars is 49. The number of bars in my result is 45. What I expected from my plot and the example plot is 44 (3*12 month + 8 month).
df_bar=df.groupby([df.date.dt.year,df.date.dt.month])[‘value’].sum().rename_axis(index=[‘year’, ‘month’]).reset_index()
fig, ax = plt.subplots(figsize=(8,6))
w1=0.6/12
for month1 in range(1,13):
dfl=df_bar[df_bar.month==month1]
ax.bar(dfl.year+w1*(month1-6), dfl.value, w1, )
ax.set_xticks(ticks=df_bar.year.unique().tolist())
ax.ticklabel_format(style=‘plain’)
ax.set_xlabel(‘Years’)
ax.set_ylabel(‘Average Page Views’)
ax.legend([calendar.month_name[i] for i in range(1, 13)], title=‘Month’)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Challenge: Data Analysis with Python Projects - Page View Time Series Visualizer
def draw_bar_plot():
# Copy and modify data for monthly bar plot
df_bar=df.groupby([df.date.dt.year,df.date.dt.month])['value'].sum().rename_axis(index=['year', 'month']).reset_index()
fig, ax = plt.subplots(figsize=(8,6))
w1=0.6/12
# for year1 in df_bar.year.unique().tolist():
for month1 in range(1,13):
dfl=df_bar[df_bar.month==month1]
ax.bar(dfl.year+w1*(month1-6), dfl.value, w1, )
ax.set_xticks(ticks=df_bar.year.unique().tolist())
ax.ticklabel_format(style='plain')
ax.set_xlabel('Years')
ax.set_ylabel('Average Page Views')
ax.legend([calendar.month_name[i] for i in range(1, 13)], title='Month')
# Save image and return fig (don't change this part)
fig.savefig('bar_plot.png')
return fig