Attribute error: AxesSubplot' object has no attribute 'savefig'

Tell us what’s happening:
I got an error message on the Time series challenge. Could anyone kindly advise? Thanks

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    time_series_visualizer.draw_bar_plot()
  File "/home/runner/boilerplate-page-view-time-series-visualizer-1/time_series_visualizer.py", line 43, in draw_bar_plot
    fig.savefig('bar_plot.png')
AttributeError: 'AxesSubplot' object has no attribute 'savefig'
exit status 1

My code is as below
Your code so far

#/ Import data (Make sure to parse dates. Consider setting index column to ‘date’.)
df = pd.read_csv(‘fcc-forum-pageviews.csv’,parse_dates=[‘date’],index_col=‘date’)

#/ Clean data
df = df.loc[(df[‘value’]>df[‘value’].quantile(0.025)) & (df[‘value’]<df[‘value’].quantile(1-0.025))]

def draw_line_plot():
# Draw line plot
fig = plt.figure(figsize = (12,5))
plt.plot(df,color=‘r’)
plt.title(“Daily freeCodeCamp Forum Page Views 5/2016-12/2019”)
plt.xlabel(“Date”)
plt.ylabel(“Page Views”)

# /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
df_bar = df.copy()
df_bar[‘year’] = df_bar.index.year
df_bar[‘month’] = df_bar.index.month
df_bar[‘month’] = pd.to_datetime(df_bar[‘month’], format=’%m’).dt.month_name()
df_bar_temp = df_bar.groupby([‘year’, ‘month’])[‘value’].mean().unstack()

#/ Draw bar plot
g = df_bar_temp.plot(kind='bar',figsize=(12,6))
g.set_xlabel('Years')
g.set_ylabel('Average Page Views')
g.legend(title= 'Months')
fig= g

#/ 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]

# /Draw box plots (using Seaborn)
fig = plt.figure(figsize=(14, 6))

ax1 = fig.add_subplot(1,2,1)
sns.boxplot(x=df_box["year"], y=df_box["value"])
ax1.set_xlabel("Year")
ax1.set_ylabel("Page Views")
ax1.set_title("Year-wise Box Plot (Trend)")

ax2 = fig.add_subplot(1,2,2)
sns.boxplot(x=df_box["month"], y=df_box["value"],order=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"])
ax2.set_xlabel("Month")
ax2.set_ylabel("Page Views")
ax2.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/77.0.3865.90 Safari/537.36.

Challenge: Page View Time Series Visualizer

Link to the challenge:

Hi… So g currently is an AxesSubplot object. The figure you need to assign as fig (the object with the .savefig method and the object you need to return) can be accessed with g.figure

Replace that line with fig = g.figure and that’s fixed! :slight_smile:

I ran the tests with your draw_bar_plot() function… The months need to be in the right order too…

Hope that helped!

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