Code works on Jupyter but not in replit Page Views Time Series Visualizer

In the draw_bar_plot function I have the following code:

df_bar = pd.pivot_table(df, values='value', index=df.index.year, columns=df.index.month) month_list = pd.period_range(start="2022-01-01", end="2022-12-31", freq='M') month_list = [month.strftime("%B") for month in month_list] df_bar.columns = month_list

In Jupyter it neatly organizes data for the expected bar plot, which also meets the criteria of the assignment. Code as follows:

fig = plt.figure() ax = df_bar.plot(kind='bar', figsize=(6,5)) ax.set_xlabel('Years') ax.set_ylabel('Average Page Views') plt.legend(title='Months', fontsize=8)

However, on replit I get the following errors:

`ERROR: test_bar_plot_labels (test_module.BarPlotTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-page-view-time-series-visualizer/test_module.py”, line 38, in setUp
self.ax = self.fig.axes[0]
IndexError: list index out of range

======================================================================
ERROR: test_bar_plot_legend_labels (test_module.BarPlotTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-page-view-time-series-visualizer/test_module.py”, line 38, in setUp
self.ax = self.fig.axes[0]
IndexError: list index out of range

======================================================================
ERROR: test_bar_plot_number_of_bars (test_module.BarPlotTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-page-view-time-series-visualizer/test_module.py”, line 38, in setUp
self.ax = self.fig.axes[0]
IndexError: list index out of range`

All pertain to the line 38, which is:
df_bar.columns = month_list

Please, kindly help me understand this difference between Jupyter and replit.

This error means that self.fig.axes is an empty list. In other words, there are no sets of axes in the figure. You’ll need to post a link your repl for actual debugging, but it looks like you are defining fig and ax as if you are using matplotlib explicitly but you are actually using the implicit interface.

As far as the difference, jupyter must do a bunch of stuff for you that a standard python environment does not do. replit is providing an online, but otherwise standard, python environment.

Thanks, Jeremy.

Here’s the link to my repl:

In the meantime, I’ll check my axes.

Hi, I finally managed to go round this error by using sns.barplot. But I still don’t know how to do it with a matplotlip ax.bar. I’d be glad if you could recommend something.

It’s probably not the use of sns.barplot() so much as it was changing this (implicit interface)

to this (explicit interface)

fig, ax = plt.subplots()

which creates the appropriate matplotlib object for the tests.

Yeah, I thoght so. But when using:

fig, ax = plt.subplots()

then I wasn’t able to create ax.bar with separate monthly values. This was the reason I went for df.plot, which did that by default.

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