Fantasy Football, Python, and Plotly Express

I'm using a really excellent package that Christian Wendt put together.

To get start and connect to your league:

# Dependencies
import requests
import pandas as pd
import numpy as np
import plotly.express as px
import matplotlib.pyplot as plt

from espn_api.football import League

league = League(league_id=xxxxx, year=2022, espn_s2='xxxxx', swid='{xxxx}')

Create an empty df and then fill it with the weekly matchups:

column_names = ["week","home_team", "home_score", "away_team","away_score"]
full_matchups = pd.DataFrame(columns = column_names)

def weekly_schedule():
for i in range(5):
global full_matchups
matchups = league.box_scores(i)
temp_df = pd.DataFrame([t.__dict__ for t in matchups ])
temp_df.drop(columns=['matchup_type', 'is_playoff', 'home_projected','home_lineup', 'away_lineup', 'away_projected'],inplace=True)
temp_df.insert(0, 'week', i)
temp_df
full_matchups = pd.concat([temp_df, full_matchups])
return full_matchups

weekly_schedule()

Do a really ugly job cleaning it up:

home_teams = full_matchups.filter(['week','home_team','home_score'], axis=1)
home_teams.rename(columns={"home_team": "team", "home_score":"score"},inplace=True)

away_teams = full_matchups.filter(['week','away_team','away_score'], axis=1)
away_teams.rename(columns={"away_team": "team", "away_score":"score"},inplace=True)

long_matchups = pd.concat([home_teams, away_teams])
long_matchups.sort_values(by=['week'],ascending=True)
long_matchups

#sorting by week
long_matchups.sort_values(by=['week'],ascending=True,inplace=True)
#adding 1 to get rid of index 0
long_matchups["week"] = long_matchups["week"] + 1
#making a string to get rid of half ticks on chart
long_matchups["week"] = long_matchups.agg('week {0[week]}'.format, axis=1)
#adding a chart

long_matchups["team"] = long_matchups["team"].astype(str)
long_matchups['team'] = long_matchups.team.str.replace('Team\(', '')
long_matchups['team'] = long_matchups.team.str.replace('\)', '')

And, finally, create the bar chart

fig = px.bar(long_matchups, x="team", y="score", color="week", title="Total points through week 5")

fig.update_layout(barmode='stack', xaxis={'categoryorder':'total descending'})
fig.show()