Skip to content

Commit

Permalink
update to seaborn 0.11.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanbraun committed Jun 16, 2021
1 parent 216d8f5 commit 540d3dc
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 48 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,24 @@ 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.9.0 (2021-06-16)
Updated visualization section + associated homework problems to use Seaborn
0.11.x (September 2020), which added a new `displot` function. This means
making our distribution plots change from, say:

```python
g = (sns.FacetGrid(df)
.map(sns.kdeplot, 'std', shade=True))
```

To:

```python
g = sns.displot(df, x='std', kind='kde', fill=True)
```

It also opens up some new possibilities (e.g. with plotting empirical CDFs)
that I might discuss in a future update.

### v0.8.0
Add this changelog, bundle files in an github release vs including with SendOwl.
67 changes: 39 additions & 28 deletions code/06_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
# stored
# on Windows it might be something like 'C:/mydir'

DATA_DIR = '/Users/nathanbraun/fantasymath/fantasybook/data'
# DATA_DIR = '/Users/nathan/fantasybook/data'
DATA_DIR = '/Users/nathan/fantasybook/data'

###############
# distributions
Expand Down Expand Up @@ -37,23 +36,15 @@
df[['player_name', 'week', 'std', 'ppr', 'half_ppr']].head()

# density plot of standard points

# all on one line
g = sns.FacetGrid(df).map(sns.kdeplot, 'std', shade=True)

# on seperate lines so it's clearer it's a two step process
g = (sns.FacetGrid(df)
.map(sns.kdeplot, 'std', shade=True))
g = sns.displot(df, x='std', kind='kde', fill=True)

# density plot of standard points by position
g = (sns.FacetGrid(df, hue='pos')
.map(sns.kdeplot, 'std', shade=True)
.add_legend())
g = sns.displot(df, x='std', kind='kde', fill=True, hue='pos')


# density plot of standard points by position and week
g = (sns.FacetGrid(df, hue='pos', col='week', col_wrap=4, height=2)
.map(sns.kdeplot, 'std', shade=True)
.add_legend())
g = sns.displot(df, x='std', kind='kde', fill=True, hue='pos', col='week',
col_wrap=4, height=2)

# now want it by scoring system

Expand All @@ -73,15 +64,28 @@ def score_type_df(_df, scoring):
[score_type_df(df, scoring) for scoring in ['std', 'ppr', 'half_ppr']])

# now can plot points by scoring system and position
g = (sns.FacetGrid(df_pts_long, col='pos', hue='scoring', col_wrap=2,
aspect=1.3)
.map(sns.kdeplot, 'pts', shade=True)
g = sns.displot(df_pts_long, x='pts', col='pos', hue='scoring', kind='kde',
fill=True, col_wrap=2, aspect=1.3)

# ecdf
g = (sns.displot(df_pts_long, x='pts', col='pos', hue='scoring', kind='ecdf',
col_wrap=2, aspect=1.3)
.add_legend())
plt.show()


#################################
# relationships between variables
#################################


# airyards vs yac
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')

# airyards vs yac
g = sns.relplot(x='caught_airyards', y='raw_yac', data=df)
g.fig.subplots_adjust(top=0.9)
Expand Down Expand Up @@ -133,15 +137,22 @@ def score_type_df(_df, scoring):
##############

# basic plot
g = (sns.FacetGrid(df_pts_long, col='pos', hue='scoring')
.map(sns.kdeplot, 'pts', shade=True))
g = sns.displot(df_pts_long, x='pts', col='pos', hue='scoring', kind='kde',
fill=True)

# wrapping columns
g = (sns.FacetGrid(df_pts_long, col='pos', hue='scoring', col_wrap=2)
.map(sns.kdeplot, 'pts', shade=True)
.fig.subplots_adjust(top=0.9) # adding a title
.fig.suptitle('Fantasy Points by Position, Scoring System')
.set_xlabels('Points') # modifying axis
.set_ylabels('Density')
.add_legend() # adding legend
.savefig('scoring_by_pos_type.png')) # saving
g = sns.displot(df_pts_long, x='pts', col='pos', hue='scoring', kind='kde',
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')

# modifying axis
g.set(xlim=(-10, 40), ylim=(0, 0.014))

g.set_xlabels('Points') # modifying axis
g.set_ylabels('Density')

# saving
g.savefig('scoring_by_pos_type.png') # saving
1 change: 0 additions & 1 deletion code/07_02_coinflip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from pandas import DataFrame
import statsmodels.formula.api as smf


coin = ['H', 'T']

# make empty DataFrame
Expand Down
35 changes: 16 additions & 19 deletions solutions-to-exercises/06_plotting_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,54 @@
# stored
# on Windows it might be something like 'C:/mydir'

DATA_DIR = '/Users/nathan/fantasybook/data'
# DATA_DIR = '/Users/nathan/fantasybook/data'
DATA_DIR = '/Users/nathanbraun/fantasymath/ltcwff-files/data'

pbp = pd.read_csv(path.join(DATA_DIR, 'play_data_sample.csv')) # play by play data

###############################################################################
# 6.1a
###############################################################################
g = (sns.FacetGrid(pbp)
.map(sns.kdeplot, 'yards_gained', shade=True))
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.savefig('./answers-to-exercises/6-1a.png')
g.savefig('./solutions-to-exercises/6-1a.png')

###############################################################################
# 6.1b
###############################################################################
g = (sns.FacetGrid(pbp.query("down <= 3"), hue='down')
.map(sns.kdeplot, 'yards_gained', shade=True))
g.add_legend()
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.savefig('./answers-to-exercises/6-1b.png')
g.savefig('./solutions-to-exercises/6-1b.png')

###############################################################################
# 6.1c
###############################################################################
g = (sns.FacetGrid(pbp.query("down <= 3"), col='down')
.map(sns.kdeplot, 'yards_gained', shade=True))
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.savefig('./answers-to-exercises/6-1c.png')
g.savefig('./solutions-to-exercises/6-1c.png')

###############################################################################
# 6.1d
###############################################################################
g = (sns.FacetGrid(pbp.query("down <= 3"), col='down', row='posteam')
.map(sns.kdeplot, 'yards_gained', shade=True))
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.savefig('./answers-to-exercises/6-1d.png')
g.savefig('./solutions-to-exercises/6-1d.png')

###############################################################################
# 6.1e
###############################################################################
g = (sns.FacetGrid(pbp.query("down <= 3"), col='down', row='posteam',
hue='posteam')
.map(sns.kdeplot, 'yards_gained', shade=True))
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.savefig('./answers-to-exercises/6-1e.png')

g.savefig('./solutions-to-exercises/6-1e.png')

###############################################################################
# 6.2
Expand Down
Binary file modified solutions-to-exercises/6-1a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified solutions-to-exercises/6-1b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified solutions-to-exercises/6-1c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified solutions-to-exercises/6-1d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified solutions-to-exercises/6-1e.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 540d3dc

Please sign in to comment.