Skip to content

Commit

Permalink
fix: avoid seaborn warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanbraun committed Jun 18, 2024
1 parent dfb8df3 commit 02d4939
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ If I were using Windows, it might look like this:
Set these aside for now and we'll pick them up in chapter 2.

## Changelog
### v0.11.0 (2024-06-18)
Update visualization code to get rid of a few seaborn warnings.

### v0.10.10 (2024-02-07)
Fix Pandas `mean` example. More: Pandas changed their defaults to throw an
error if you try to call this on string columns. Fixed example to explicitely
Expand Down
26 changes: 13 additions & 13 deletions code/06_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
df[['pos', 'std', 'ppr', 'half_ppr']].head() # have this

def score_type_df(_df, scoring):
_df = _df[['pos', scoring]]
_df = _df[['pos', scoring]].copy()
_df.columns = ['pos', 'pts']
_df['scoring'] = scoring
return _df
Expand Down Expand Up @@ -84,18 +84,18 @@ def score_type_df(_df, scoring):
g = sns.displot(x='caught_airyards', y='raw_yac', data=df, kind='kde')
plt.show()

g.fig.subplots_adjust(top=0.9)
g.fig.suptitle('Airyards vs YAC')
g.figure.subplots_adjust(top=0.9)
g.figure.suptitle('Airyards vs YAC')

# airyards vs yac
g = sns.relplot(x='caught_airyards', y='raw_yac', data=df)
g.fig.subplots_adjust(top=0.9)
g.fig.suptitle('Airyards vs YAC')
g.figure.subplots_adjust(top=0.9)
g.figure.suptitle('Airyards vs YAC')

# airyards vs yac - colored yb position
g = sns.relplot(x='caught_airyards', y='raw_yac', hue='pos', data=df)
g.fig.subplots_adjust(top=0.9)
g.fig.suptitle('Airyards vs YAC - by Position')
g.figure.subplots_adjust(top=0.9)
g.figure.suptitle('Airyards vs YAC - by Position')

# correlation
df.loc[df['pos'] == 'WR',
Expand Down Expand Up @@ -123,15 +123,15 @@ def score_type_df(_df, scoring):

g = sns.relplot(x='week', y='std', kind='line', hue='pos', style='pos',
data=max_pts_by_pos_week, height=4, aspect=1.2)
g.fig.subplots_adjust(top=0.9)
g.fig.suptitle("Max Points by Week and Position")
g.figure.subplots_adjust(top=0.9)
g.figure.suptitle("Max Points by Week and Position")

# points by player and week
g = sns.relplot(x='week', y='std', kind='line', hue='player_name',
col='player_name', height=2, aspect=1.2, col_wrap=3,
legend=False, data=df.query("pos == 'QB'"))
g.fig.subplots_adjust(top=0.9)
g.fig.suptitle("Points by Week = QBs")
g.figure.subplots_adjust(top=0.9)
g.figure.suptitle("Points by Week = QBs")

##############
# plot options
Expand All @@ -146,8 +146,8 @@ def score_type_df(_df, scoring):
fill=True, col_wrap=2)

# adding a title
g.fig.subplots_adjust(top=0.9) # adding a title
g.fig.suptitle('Fantasy Points by Position, Scoring System')
g.figure.subplots_adjust(top=0.9) # adding a title
g.figure.suptitle('Fantasy Points by Position, Scoring System')

# modifying axis
g.set(xlim=(-10, 40), ylim=(0, 0.014))
Expand Down
24 changes: 12 additions & 12 deletions solutions-to-exercises/06_plotting_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,44 @@
# 6.1a
###############################################################################
g = sns.displot(pbp, x='yards_gained', kind='kde', fill=True)
g.fig.subplots_adjust(top=0.9)
g.fig.suptitle('Distribution of Yards Gained Per Play, LTCWFF Sample')
g.figure.subplots_adjust(top=0.9)
g.figure.suptitle('Distribution of Yards Gained Per Play, LTCWFF Sample')
g.savefig('./solutions-to-exercises/6-1a.png')

###############################################################################
# 6.1b
###############################################################################
g = sns.displot(pbp.query("down <= 3"), x='yards_gained', kind='kde',
hue='down', fill=True)
g.fig.subplots_adjust(top=0.9)
g.fig.suptitle('Distribution of Yards Gained Per Play by Down, LTCWFF Sample')
g.figure.subplots_adjust(top=0.9)
g.figure.suptitle('Distribution of Yards Gained Per Play by Down, LTCWFF Sample')
g.savefig('./solutions-to-exercises/6-1b.png')

###############################################################################
# 6.1c
###############################################################################
g = sns.displot(pbp.query("down <= 3"), x='yards_gained', kind='kde',
col='down', fill=True)
g.fig.subplots_adjust(top=0.8)
g.fig.suptitle('Distribution of Yards Gained Per Play by Down, LTCWFF Sample')
g.figure.subplots_adjust(top=0.8)
g.figure.suptitle('Distribution of Yards Gained Per Play by Down, LTCWFF Sample')
g.savefig('./solutions-to-exercises/6-1c.png')

###############################################################################
# 6.1d
###############################################################################
g = sns.displot(pbp.query("down <= 3"), x='yards_gained', kind='kde',
row='posteam', col='down', fill=True)
g.fig.subplots_adjust(top=0.9)
g.fig.suptitle('Distribution of Yards Gained Per Play by Down, Team, LTCWFF Sample')
g.figure.subplots_adjust(top=0.9)
g.figure.suptitle('Distribution of Yards Gained Per Play by Down, Team, LTCWFF Sample')
g.savefig('./solutions-to-exercises/6-1d.png')

###############################################################################
# 6.1e
###############################################################################
g = sns.displot(pbp.query("down <= 3"), x='yards_gained', col='down',
row='posteam', hue='posteam', fill=True)
g.fig.subplots_adjust(top=0.9)
g.fig.suptitle('Distribution of Yards Gained Per Play by Down, Team, LTCWFF Sample')
g.figure.subplots_adjust(top=0.9)
g.figure.suptitle('Distribution of Yards Gained Per Play by Down, Team, LTCWFF Sample')
g.savefig('./solutions-to-exercises/6-1e.png')

###############################################################################
Expand All @@ -66,8 +66,8 @@
# relationships
pg = pd.read_csv(path.join(DATA_DIR, 'player_game_2017_sample.csv'))
g = sns.relplot(x='carries', y='rush_yards', hue='pos', data=pg)
g.fig.subplots_adjust(top=0.9)
g.fig.suptitle('Carries vs Rush Yards by Position, LTCWFF Sample')
g.figure.subplots_adjust(top=0.9)
g.figure.suptitle('Carries vs Rush Yards by Position, LTCWFF Sample')
g.savefig('./answers-to-exercises/6-2.png')

pg['ypc'] = pg['rush_yards']/pg['carries']
Expand Down

0 comments on commit 02d4939

Please sign in to comment.