Am trying to use seaborn to do a subplot, and i goy the message AttributeError: 'Figure' object has no attribute 'boxplot'

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]
axes = plt.subplots(nrows =1, ncols =2, figsize=(12,5))
#= plt.figure(figsize=(12,5))
#fig1 = plt.subplot(1,2,1)
axes[0] = sns.boxplot(ax = axes[0], data=df_box, x=df_box["year"], y=df_box["value"])
axes[0].set_title("Year-Wise Box Plot(Trend)")
axes[0].set(xlabel="Year", ylabel = "Page views")
#fig2= plt.figure(figsize=(12,5))
#fig2 = plt.subplot(1,2,2)
Order =["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
axes[1] = sns.boxplot(ax = axes[1], data=df_box, x=df_box["month"], y=df_box["value"], order =["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"])
axes[1].set_title("Month-Wise Box Plot(Seasonality)")
axes[1].set(xlabel="Month", ylabel = "Page views")
plt.show()

Could you paste the complete code? Could you paste the exact error (traceback)?

1 Like

Yes sanity, I have solved that particular problem. But can you help me with this.

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", index_col = ["date"], parse_dates = True)

# 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 = plt.figure(figsize = (6,6))
    plt.plot( df["value"], "r")
    plt.title("Daily freeCodeCamp Forum Page Views 5/2016-12/2019")
    plt.xlabel("Date")
    plt.ylabel("Page Views")
    #plt.show()
    
    # 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["month"]=df.index.month
    df["years"]= df.index.year
    df["day"] = df.index.day
    df_bar = df.groupby(["years", "month"])["value"].mean()
    df_bar = df_bar.unstack()
    

    # Draw bar plot
    fig = plt.figure(figsize = (10,5))
    #df_bar.plt.bar()
    fig =  df_bar.plot.bar()
    plt.xlabel("Years")
    plt.ylabel("Average Page Views")
    Months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    plt.legend( title ="Months", labels = Months)
    #plt.show()

    # 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)
    df_box["month_no"] = df_box['date'].dt.month
    df_box = df_box.sort_values("month_no")
    fig, (ax1,ax2) = plt.subplots(1,2, figsize=(12,5))
    ax1 = sns.boxplot(ax = ax1, data=df_box, x=df_box["year"], y=df_box["value"])
    ax1.set_title("Year-wise Box Plot (Trend)")
    ax1.set(xlabel="Year", ylabel = "Page Views")
    ax2 = sns.boxplot(ax = ax2, data=df_box, x=df_box["month"], y=df_box["value"])
    ax2.set_title("Month-wise Box Plot (Seasonality)")
    ax2.set(xlabel="Month", ylabel = "Page Views")
    #plt.show()
    
    # Save image and return fig (don't change this part)
    fig.savefig('box_plot.png')
    return fig

Here is the error message

 python main.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-b89cg4gs because the default path (/config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
EEE....E...
======================================================================
ERROR: test_bar_plot_labels (test_module.BarPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-page-view-time-series-visualizer-3/test_module.py", line 38, in setUp
    self.ax = self.fig.axes[0]
TypeError: 'AxesSubplot' object is not subscriptable

======================================================================
ERROR: test_bar_plot_legend_labels (test_module.BarPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-page-view-time-series-visualizer-3/test_module.py", line 38, in setUp
    self.ax = self.fig.axes[0]
TypeError: 'AxesSubplot' object is not subscriptable

======================================================================
ERROR: test_bar_plot_number_of_bars (test_module.BarPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-page-view-time-series-visualizer-3/test_module.py", line 38, in setUp
    self.ax = self.fig.axes[0]
TypeError: 'AxesSubplot' object is not subscriptable

======================================================================
ERROR: test_data_cleaning (test_module.DataCleaningTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-page-view-time-series-visualizer-3/test_module.py", line 7, in test_data_cleaning
    actual = int(time_series_visualizer.df.count(numeric_only=True))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/series.py", line 185, in wrapper
    raise TypeError(f"cannot convert the series to {converter}")
TypeError: cannot convert the series to <class 'int'>

----------------------------------------------------------------------
Ran 11 tests in 7.585s

FAILED (errors=4)

Looks like pandas’s method plot might not be returning what is expected by tests.