Time_series_visualizer

My code is running properly in google colab but throwing the error below in replit

File “main.py”, line 6, in

time_series_visualizer.draw_line_plot()

File “/home/runner/boilerplate-page-view-time-series-visualizer-1/time_series_visualizer.py”, line 24, in draw_line_plot
fig.savefig(‘line_plot.png’)
AttributeError: ‘AxesSubplot’ object has no attribute ‘savefig’##

**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’, parse_dates=True, index_col=‘date’)

Clean data

df = df[(df[‘value’] >= df[‘value’].quantile(0.025)) & (df[‘value’] <= df[‘value’].quantile(0.975))]

def draw_line_plot():
# Draw line plot
fig = sns.lineplot(data = df, x=‘date’, y= ‘value’, color=‘red’)
fig.set_xlabel(‘Date’)
fig.set_ylabel(‘Page Views’)
fig.set_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

def draw_bar_plot():
# Copy and modify data for monthly bar plot
Months = [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’, ‘Jul’, ‘Aug’, ‘Sep’, ‘Oct’, ‘Nov’, ‘Dec’]
df[‘Months’] = df.index.month
df[‘year’] = df.index.year
df[“Months”] = df[“Months”].apply(lambda x: months[x-1])
df[“Months”] = pd.Categorical(df[“Months”], categories=months)
df2 =pd.pivot_table(df, index = ‘year’, columns=‘Months’, values = ‘value’, aggfunc=np.mean)

# Draw bar plot
bar = df2.plot(kind='bar')
bar.set_xlabel('Years')
bar.set_ylabel('Average Page Views')




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

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]
df_box[“month”] = pd.Categorical(df_box[“month”], categories=months)
df_box

# Draw box plots (using Seaborn)
fig, (ax_year1, ax_month1) = plt.subplots(1, 2)
fig.set_figwidth(15)
fig.set_figheight(7)

ax_year1 = sns.boxplot(data = df_box, x='year', y='value', ax=ax_year1)
ax_year1.set_xlabel('Year')
ax_year1.set_ylabel('Page Views')
ax_year1.set_title("Year-wise Box Plot (Trend)")

ax_month1 = sns.boxplot(x="month", y="value", data=df_box, ax=ax_month1)
ax_month1.set_xlabel('Month')
ax_month1.set_ylabel('Page Views')
ax_month1.set_title("Month-wise Box Plot (Seasonality)")




# 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: Page View Time Series Visualizer

Link to the challenge:

Hi…

That line assigns an AxisSubplot object to variable fig… AxesSubplot objects don’t have a .savefig method - the object at the AxesSubplotObject.figure has it. That’s also the object you’re looking to return from the function…

You could solve this particular error by adding a fig = fig.figure to your code before the .savefig and return lines…

Hope that helps!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.