Christian Fett
Christian Fett

Reputation: 39

Plotting Multiple Lines Graph from DataFrame

I have a large data set containing years, NFL teams, their total salaries paid out for that year, and more misc stats. I want to create a plot that has the years on the x-axis, total salaries on the y and then has multiple lines, one for each team.

The data I want to plot looks something like this except there are of course way more teams and years and the total salaries are accurate:

Year Team Salaries
2015 Miami $100
2015 Denver $150
2015 LA $125
2016 Miami $125
2016 Denver $100
2016 LA $100

I know pandas plot function and I can set the x-axis but when I set y to be total salaries it just gives me a single line. I also do not know how to set it to break up the data by each team so each team is treated as a separate line.

Upvotes: 1

Views: 7306

Answers (2)

Nk03
Nk03

Reputation: 14949

Alternative via seaborn:

import seaborn as sns

import pandas as pd

df = pd.DataFrame(
    {
        "Year": ["2015", "2016", "2017", "2018"] * 6,
        "Team": ["Miami", "Denver", "LA"] * 8,
        "Salaries": [100, 200, 150, 125, 100, 250] * 4,
    }
)


sns.lineplot(x='Year', y='Salaries', hue='Team', data=df)

OUTPUT:

enter image description here

NOTE: Thanks to @Cornelius Roemer for the model data.

Upvotes: 4

Cornelius Roemer
Cornelius Roemer

Reputation: 8346

You want to use a pivot table to get a new column per team.

Once you've got the data reshaped like this, plotting is easy. Check out the documentation on pivot tables.

import pandas as pd
df = pd.DataFrame(
     {
         "Year": ["2015", "2016", "2017", "2018"] * 6,
         "Team": ["Miami", "Denver", "LA"] * 8,
         "Salaries": [100, 200, 150, 125, 100, 250] * 4,
     }
)

df.pivot_table(values="Salaries",index="Year",columns="Team").plot()

The result of the pivot table looks like this

Team    Denver  LA  Miami
Year            
2015    100 150 100
2016    200 250 125
2017    100 150 100
2018    200 250 125

And the plot: enter image description here

Upvotes: 5

Related Questions