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