0

Im am trying to plot multiple lines with their corresponding legend:

regions = ['Wales', 'Scotland', 'London', 'East of England', 'East Midlands',
       'Yorkshire and The Humber', 'South East', 'South West',
       'West Midlands', 'North West', 'North East']

plt.figure(figsize = (10,8))
plt.title('Number of Vehicles per Region')
plt.xlabel('Year')
plt.ylabel('Number of Vehicles')
plt.legend()

for i in regions:
    region = raw_miles_df.loc[i].sum(axis = 1).reset_index()

    region = region.rename(columns = {'count_date':'Year', 0: 'vehicles'})

    region['Year'] = region['Year'].apply(lambda x: x.year)
    region = region.groupby(['Year']).agg(vehicles = ('vehicles', lambda x: x.mean().round(2)))

    plt.plot(region)
    plt.legend(i)

the method i have is not working:

enter image description here

JNix
  • 37
  • 1
  • 5
  • Please [provide a reproducible copy of the DataFrame with `to_clipboard`](https://stackoverflow.com/questions/52413246/provide-a-reproducible-copy-of-the-dataframe-with-to-clipboard/52413247#52413247) – Trenton McKinney Apr 20 '20 at 17:30

1 Answers1

0

You need to move plt.legend out of the loop and make it plt.legend(regions). As you can see in the legend, it is treating the string 'North East', which is the last item in regions, as an iterable from which to draw the categories.

But you can make it easier on yourself by using seaborn

import seaborn as sns
# aggregate your data outside of the loop
# then call lineplot
aggdata = df.groupby(...)
sns.lineplot(x=x_column, y=y_column, hue=category_column, data=aggdata)
Eric Truett
  • 2,970
  • 1
  • 16
  • 21