TheHedge
TheHedge

Reputation: 127

How to draw plots on Specific pandas columns

So I have the df.head() being displayed below.I wanted to display the progression of salaries across time spans.As you can see the teams will get repeated across the years and the idea is to display how their salaries changed over time.So for teamID='ATL' I will have a graph that starts by 1985 and goes all the way to the present time. I think I will need to select teams by their team ID and have the x axis display time (year) and Y axis display year. I don't know how to do that on Pandas and for each team in my data frame.

   teamID yearID lgID payroll_total franchID    Rank    W       G        win_percentage
0   ATL   1985   NL   14807000.0    ATL         5       66      162      40.740741
1   BAL   1985   AL   11560712.0    BAL         4       83      161      51.552795
2   BOS   1985   AL   10897560.0    BOS         5       81      163      49.693252
3   CAL   1985   AL   14427894.0    ANA         2       90      162      55.555556
4   CHA   1985   AL   9846178.0     CHW         3       85      163      52.147239
5   ATL   1986   NL   17800000.0    ATL         4       55      181      41.000000 

Upvotes: 1

Views: 159

Answers (2)

dapaz
dapaz

Reputation: 813

import pandas as pd
import matplotlib.pyplot as plt

# Display the box plots on 3 separate rows and 1 column
fig, axes = plt.subplots(nrows=3, ncols=1)

# Generate a plot for each team
df[df['teamID'] == 'ATL'].plot(ax=axes[0], x='yearID', y='payroll_total')
df[df['teamID'] == 'BAL'].plot(ax=axes[1], x='yearID', y='payroll_total')
df[df['teamID'] == 'BOS'].plot(ax=axes[2], x='yearID', y='payroll_total')  

# Display the plot
plt.show()

depending on how many teams you want to show you should adjust the

fig, axes = plt.subplots(nrows=3, ncols=1)

Finally, you could create a loop and create the visualization for every team

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150745

You can use seaborn for this:

import seaborn as sns

sns.lineplot(data=df, x='yearID', y='payroll_total', hue='teamID')

To get different plot for each team:

for team, d in df.groupby('teamID'):
    d.plot(x='yearID', y='payroll_total', label='team')

Upvotes: 1

Related Questions