Page View Time Series Visualizer:Barplot

Not able to plot bar graph

**def draw_bar_plot():

Copy and modify data for monthly bar plot

df["month"]= df.index.month
df["year"]= df.index.year
df= df.groupby(["year","month"])["value"].mean().unstack()

Draw bar plot

df.plot.bar()
plt.xlabel(“Years”, fontsize=10)
plt.ylabel(“Average Page Views”, fontsize=10)
plt.xlabel(“Years”, fontsize= 10)
plt.ylabel(“Average Page Views”, fontsize= 10)
#plt.xticks(rotation=30)
plt.xticks(fontsize = 10)
plt.yticks(fontsize = 10)
plt.legend(fontsize = 10, labels = [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’])
**

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36.

Challenge: Page View Time Series Visualizer

Link to the challenge:
https://www.freecodecamp.org/learn/data-analysis-with-python/data-analysis-with-python-projects/page-view-time-series-visualizerPreformatted text

You should try running your code line by line in a notebook environment and looking at what the output is. For example, running df["month"]= df.index.month results in

AttributeError: 'RangeIndex' object has no attribute 'month'

at least based on no prior information to what you have done formatting wise. So I can’t really help you with the specific code that you provided.

In any case, the base code suggests that you make a copy

df_bar = df.copy().reset_index(inplace=True)

(this is given in the subsequent function). Now you can use the DatetimeIndex method to convert the dates to the right format and extract the month and year values into separate columns

df_bar['year']=pd.DatetimeIndex(df_bar['date']).year
df_bar['month']=pd.DatetimeIndex(df_bar['date']).month_name()

From here, can use sns.catplot to plot your data (examples can be found in the documentation for Seaborn). You will need to sort df_bar by the month number (you might want to create a new column to help you with that) to make the bars appear in the right order.

``
Below is the code that I am trying to implement. I tried your solution as well but still not working. Please help me out.`
**

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[(df[“value”]>=df[“value”].quantile(0.025)) & (df[“value”]<=df[“value”].quantile(0.975))]

Draw line plot

fig, ax= plt.subplots(figsize=(10,5))
ax.plot(df.index, df[“value”], color= “red”, linewidth=2)
ax.set_title(“Daily freeCodeCamp Forum Page Views 5/2016-12/2019”)
ax.set_xlabel(“Date”, fontsize=10)
ax.set_ylabel(“Page Views”, fontsize=10)

Couldnt find OOP approach of setting ticks fontsize

plt.xticks(fontsize=10)
plt.yticks(fontsize=10)

Save image and return fig (don’t change this part)

fig.savefig(‘line_plot.png’)
fig
def draw_bar_plot():

Copy and modify data for monthly bar plot

df_bar = df.copy().reset_index(inplace=True)
df_bar['year']=pd.DatetimeIndex(df_bar['date']).year
df_bar['month']=pd.DatetimeIndex(df_bar['date']).month_name()

Draw bar plot

df_bar.plot.bar()
plt.xlabel(“Years”, fontsize=10)
plt.ylabel(“Average Page Views”, fontsize=10)
plt.xlabel(“Years”, fontsize= 10)
plt.ylabel(“Average Page Views”, fontsize= 10)
#plt.xticks(rotation=30)
plt.xticks(fontsize = 10)
plt.yticks(fontsize = 10)
plt.legend(fontsize = 10, labels = [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’])

Running df_bar.plot.bar() will give you an error, because you haven’t defined the arguments.

https://seaborn.pydata.org/generated/seaborn.catplot.html
Look through the examples and the options you can set in them, such as kind and hue.

So your plotting code will look something like this (fill in the missing stuff):

g = sns.catplot(
    x='year', y='value', data = df_bar,
    kind = '?', hue='?', *more options that you can look up*
)
g.set(xlabel = '?')
g.set(ylabel = '?')
g.add_legend(title = 'Months')

Also, type your code here like this
```
code
```
This will make it look like this

code

and make it easier to read.

1 Like

have you tried removing reset_index() when copying the original df and placing it on a new line?

It will be much easier to help and debug if you can post a link to a live project, such as on repl.it or the like, so others can run the program and tests.