Home > Software design >  How to plot multiple times series using pandas and seaborn
How to plot multiple times series using pandas and seaborn

Time:01-20

I've got a dataframe of data that people have helpfully collate.

It looks like this (ignore the index, I'm just sampling):

    uni                             score   year
18  Arden University Limited        78.95   2020
245 The University of Manchester    71.35   2022
113 Darlington College              93.33   2020
94  City of Wolverhampton College   92      2017
345 The Royal Veterinary College    94      2018
118 Darlington College              62      2018

There is more data - enter image description here I can also plot the overall mean using:

df.groupby("year").mean()['score'].plot()

What I want to be able to do is plot both together.

enter image description here Ideally, I'd also like to be able to plot multiple lines in one plot and specify the colour. So for instance say the national score is in red and a particular line was say blue, while other plots were gray.

Any ideas?

CodePudding user response:

You can use the Axis to plot returned by the first call to plot and reuse it in your function:

def plot_uni(df, uni, query, ax):  # <- HERE
    print(query)
    df['query'] = df[uni].str.contains(query)
    subset = df[df['query']].set_index("year")
    subset.sort_index().plot(ax=ax)  # <- HERE

# General plot
ax = df.groupby("year")['score'].mean().plot()

plot_uni(df, 'uni', 'College', ax)  # other plots
plot.uni(df, 'uni', 'University', ax)  # and so on

CodePudding user response:

If you use the matplotlib.pyplot way to plotting instead of pandas built-in interface for it, you can simply add more lines by repeatedly calling plt.plot(data). Once you have called all your data, you do plt.show() to generate the output.

import matplotlib.pyplot as plt

def plot_uni(df, uni, query):
    print(query)
    df['query'] = df[uni].str.contains(query)
    subset = df[df['query']].set_index("year")
    plt.plot(subset.sort_index())

# Here goes some iterator that calls plot_uni
plt.show()
  • Related