Plot average curve for multiple graphs

Viewed 274

I'm a beginner in matplotlib. I've already created some graphs from a dataframe. Now I want to create the average graph from multiple graphs.

Can anyone help me with this?

My graphs look like this:

alb = df[df.location == 'Albania']
bel = df[df.location == 'Belarus']
bih = df[df.location == 'Bosnia and Herzegovina']
blg = df[df.location == 'Bulgaria']
cro = df[df.location == 'Croatia']
hun = df[df.location == 'Hungary']
kos = df[df.location == 'Kosovo']
mld = df[df.location == 'Moldova']
mng = df[df.location == 'Montenegro']
nmc = df[df.location == 'North Macedonia']
pol = df[df.location == 'Poland']
rom = df[df.location == 'Romania']
rus = df[df.location == 'Russia']
srb = df[df.location == 'Serbia']
tur = df[df.location == 'Turkey']
ukr = df[df.location == 'Ukraine']

plt.plot(alb.date, alb.new_deaths_per_million)
plt.plot(bel.date, bel.new_deaths_per_million)
plt.plot(bih.date, bih.new_deaths_per_million)
plt.plot(blg.date, blg.new_deaths_per_million)
plt.plot(cro.date, cro.new_deaths_per_million)
plt.plot(hun.date, hun.new_deaths_per_million)
plt.plot(kos.date, kos.new_deaths_per_million)
plt.plot(mld.date, mld.new_deaths_per_million)
plt.plot(mng.date, mng.new_deaths_per_million)
plt.plot(nmc.date, nmc.new_deaths_per_million)
plt.plot(pol.date, pol.new_deaths_per_million)
plt.plot(rom.date, rom.new_deaths_per_million)
plt.plot(rus.date, rus.new_deaths_per_million)
plt.plot(srb.date, srb.new_deaths_per_million)
plt.plot(tur.date, tur.new_deaths_per_million)
plt.plot(ukr.date, ukr.new_deaths_per_million)
plt.legend(['Albania', 'Belarus', 'Bosnia and Herzegovina', 'Bulgaria', 'Croatia', 'Hungary', 'Kosovo', 'Moldova', 'Montenegro', 'North Macedonia', 'Poland', 'Romania', 'Russia', 'Serbia', 'Turkey', 'Ukraine'])
plt.rcParams['figure.figsize'] = [12, 8]
plt.rcParams['figure.dpi'] = 200
plt.show()

The graphs look like this:

enter image description here

Now I would like to create the average curve from all these graphs. How can this be done?

Thanks for the help

This is the dataset I use:

url = 'https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv'
df = pd.read_csv(url,error_bad_lines=False)
pd.options.display.max_columns = None
display(df)
1 Answers
  • Put your countries in an array, it makes your life easier, your code shorter and easier to maintain

  • I would convert the dates to datetime objects and then get the weekly average. The graph looks less noisy and is easier to interpret. Calculating the average with missing values is also tricky.

    df['date'] = pd.to_datetime(df['date'])

    df.groupby(pd.Grouper(freq='W', key='date'))['new_deaths_per_million'].mean().reset_index()

  • Then group your data by date and calculate the mean. The mean values can the be plotted.

enter image description here

    import pandas as pd
    import matplotlib.pyplot as plt
    
    url = 'https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv'
    df = pd.read_csv(url,error_bad_lines=False)
    df['date'] = pd.to_datetime(df['date'])

    countries = ['Albania', 'Belarus', 'Bosnia and Herzegovina', 'Bulgaria', 'Croatia', 'Hungary', 'Kosovo', 'Moldova', 'Montenegro', 'North Macedonia', 'Poland', 'Romania', 'Russia', 'Serbia', 'Turkey', 'Ukraine']


    for country in countries:
        df_country = df[df.location == country].groupby(pd.Grouper(freq='W', key='date'))['new_deaths_per_million'].mean().reset_index()
        assert len(df_country) > 0
        plt.plot(df_country.date, df_country.new_deaths_per_million)

    df_countries = df[df.location.isin(countries)].groupby(pd.Grouper(freq='W', key='date'))['new_deaths_per_million'].mean().reset_index()

    plt.plot(df_countries['date'], df_countries['new_deaths_per_million'], linewidth=4)
    plt.legend([*countries, 'average'])

    plt.show()
Related