Created
January 3, 2021 16:55
-
-
Save MemphisMeng/7405e6f639664c85f9f63234e7589bb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def scatterit(week): | |
""" | |
Filters and plot the dataframe as a scatter plot of teams' defence performance | |
Args: | |
----- | |
* week (str): the period to filter on, or "Overall" to display the entire season | |
Returns: | |
-------- | |
A matplotlib scatter plot | |
""" | |
plot = df.copy() | |
if week != 'Overall': | |
week_num = int(week.split(' ')[1]) | |
plot = plot[plot.week == week_num] | |
defence = plot[plot['passResult'] != 'C'].groupby('defensiveTeam').agg({'passResult': 'count'}) / plot.groupby('defensiveTeam').agg({'passResult': 'count'}) | |
# Plot it (only if there's data to plot) | |
if len(plot) > 0: | |
fig, ax = plt.subplots(figsize=(15, 15)) | |
ax.scatter([i+1 for i in range(defence.shape[0])], defence['passResult'].tolist()) | |
ax.set_ylabel('Unallowed Passes %', size=15) | |
ax.set_xticklabels([]) | |
ax.set_title('Team Defence Performance', size=15) | |
for x0, y0, path in zip([i+1 for i in range(defence.shape[0])], defence['passResult'].tolist(), | |
['../input/nfl-team-logos/' + file for file in sorted(os.listdir('../input/nfl-team-logos'))]): | |
ab = AnnotationBbox(getImage(path), (x0, y0), frameon=False) | |
ax.add_artist(ab) | |
else: | |
print("No data to show for current selection") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment