-1

I am trying to change the legend on this bar chart from numbers into the months of the year but I want to keep the months ordered (i.e. from January to December).

image of current output

This is what I've tried and the legend doesn't change when taking the last line of code out or putting it in. I have also tried using legend = (['January','February',...)] as an argument in .plot.bar().

df = pd.read_csv('fcc-forum-pageviews.csv', index_col = 'date')
df.index = pd.to_datetime(df.index)
df_group = df.groupby(['Years', 'Months'])['value'].mean()
df_group = df_group.unstack()
fig = df_group.plot.bar(figsize = (10,7), legend = True, ylabel = 'Average Page Views', xlabel = 'Years').figure
plt.legend = (['January','February','March','April','May','June','July','August', 'September', 'October', 'November','December'])

It also might be worth mentioning that I am testing this in jupyterlabs. I am new to matplotlib and pandas so apologies if this seems stupid.

This is task 2 for reference:

freecodecamp GitHub page for this project

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
noel8_8
  • 1
  • 2
  • `df['Month'] = df.index.month_name()` and `df['Month'] = pd.Categorical(df.Month, month_name[1:], ordered=True)` (use `from calendar import month_name` for an ordered list of months). The default colormap has an insufficient number of colors (e.g. blue and orange or used for two categories). – Trenton McKinney Sep 01 '23 at 18:32
  • [code and plot](https://i.stack.imgur.com/eN5Eu.png) the data should be cleaned in the dataframe, not by updating the legend labels as an afterthought. – Trenton McKinney Sep 01 '23 at 18:41

0 Answers0