0

I have a below data frame so5b (not complete).

The dataframe has SO2 values per month from year 2000 to 2015

    year    month   so2
1   2000    11  17.3
2   2000    12  14.488888888888892
3   2001    1   14.377777777777778
4   2001    2   17.555555555555557
5   2001    3   13.955555555555556
6   2001    4   12.299999999999999
7   2001    5   11.0
8   2001    6   11.81111111111111
9   2001    7   10.525
10  2001    8   11.512500000000003
11  2001    9   11.212499999999999
12  2001    10  12.1625
13  2001    11  14.2125
14  2001    12  13.4875

I want to plot multiple line chart for - monthly trend of so2 values by month- for each year. i.e the output needs to be like below

Required output chart

Now to get the above plot I tried below as referenced in multiple links

so5b.info()
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
for key, grp in so5b.groupby(['year']):
    ax = grp.plot(ax=ax, kind='line', x='month', y='so2', c=key, label=key)
    plt.show()

When i execute the above I am getting the below error

ValueError: Invalid RGBA argument: 2000

Also am not sure whether I should be doing group by year i.e. so5b.groupby(['year']) since data is alredy ordered per month per year.

I refered multiple links like one below , but it dosnt seem to solve my problem

ValueError: Invalid RGBA argument: What is causing this error?

Help appreciated. TIA

smokinjoe
  • 93
  • 2
  • 8

1 Answers1

3

You can either drop c=key in the plot command. That's where error comes from as for key=2000, plt does not understand color code 2000. So:

fig, ax = plt.subplots(figsize=(10,6))
for key, grp in so5b.groupby('year'):
    ax = grp.plot(ax=ax, kind='line', x='month', y='so2', label=key)
plt.show()

gives you:

enter image description here

Or you can use seaborn:

fig, ax = plt.subplots(figsize=(10,6))
sns.lineplot(x='month', y='so2', hue='year', data=so5b)
plt.show()
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74