import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as patches
%matplotlib inline
def create_football_field():
# Create a rectangle defined via an anchor point *xy* and its *width* and *height*
rect = patches.Rectangle((0, 0), 120, 53.3, facecolor='darkgreen', zorder=0)
# Creating a subplot to plot our field on
fig, ax = plt.subplots(1, figsize=(12, 6.33))
# Adding the rectangle to the plot
ax.add_patch(rect)
# Plotting a line plot for marking the field lines
plt.plot([10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 60, 60, 70, 70, 80,
80, 90, 90, 100, 100, 110, 110, 120, 0, 0, 120, 120],
[0, 53.3, 53.3, 0, 0, 53.3, 53.3, 0, 0, 53.3, 53.3, 0, 0, 53.3, 53.3,
0, 0, 53.3, 53.3, 0, 0, 53.3, 53.3, 53.3, 0, 0, 53.3],
color='white', zorder = 0)
# Creating the left end-zone
left_end_zone = patches.Rectangle((0, 0), 10, 53.3, facecolor='blue', alpha=0.2, zorder=0)
# Creating the right end-zone
right_end_zone = patches.Rectangle((110, 0), 120, 53.3, facecolor='blue', alpha=0.2, zorder=0)
# Adding the patches to the subplot
ax.add_patch(left_end_zone)
ax.add_patch(right_end_zone)
# Setting the limits of x-axis from 0 to 120
plt.xlim(0, 120)
# Setting the limits of y-axis from -5 to 58.3
plt.ylim(-5, 58.3)
# Removing the axis values from the plot
plt.axis('off')
# Plotting the numbers starting from x = 20 and ending at x = 110
# with a step of 10
for x in range(20, 110, 10):
# Intializing another variable named 'number'
number = x
# If x exceeds 50, subtract it from 120
if x > 50:
number = 120 - x
# Plotting the text at the bottom
plt.text(x, 5, str(number - 10),
horizontalalignment='center',
fontsize=20,
color='white')
# Plotting the text at the top
plt.text(x - 0.95, 53.3 - 5, str(number - 10),
horizontalalignment='center',
fontsize=20,
color='white',
rotation=180)
# Making ground markings
for x in range(11, 110):
ax.plot([x, x], [0.4, 0.7], color='white', zorder = 0)
ax.plot([x, x], [53.0, 52.5], color='white', zorder = 0)
ax.plot([x, x], [22.91, 23.57], color='white', zorder = 0)
ax.plot([x, x], [29.73, 30.39], color='white', zorder = 0)
# Returning the figure and axis
return fig, ax
# Calling the plotting function
fig, ax = create_football_field()
# Plotting the figure
plt.show()
# Reading the data as a Pandas DataFrame
df = pd.read_csv('week_data.csv')
# Looking at the first five rows of the DataFrame
df.head()
# Looking at the shape of the DataFrame
df.shape
# Converting to Time values
df['time'] = pd.to_datetime(df['time']).dt.time
# Looking at the first five rows of the DataFrame
df.head()
# Sorting the values of the DataFrame by time in an ascending order
df = df.sort_values(by='time', ascending=True).reset_index(drop=True)
# Looking at the first five rows of the DataFrame
df.head()
# Selecting the data for the given game and play based on their Id
sel_df = df.query('gameId == 2018111900 and playId == 5577')
# Looking at the shape of the DataFrame
print(f'The shape of the DataFrame is: {sel_df.shape}')
# Looking at the DataFrame
sel_df
# Selecting the home and away team
home_team = sel_df.query('team == "home"')
away_team = sel_df.query('team == "away"')
# Selecting the football
football = sel_df.query('team == "football"')
# Creating the football field
fig, ax = create_football_field()
# Plotitng the home team
home_team.plot(x='x', y='y', kind='scatter', ax=ax, color='blue', s=20, zorder=2)
# Plotting the away team
away_team.plot(x='x', y='y', kind='scatter', ax=ax, color='orange', s=20, zorder=2)
# Plotting the football
football.plot(x='x', y='y', kind='scatter', ax=ax, color='brown', s=20, zorder=2)
# Displaying the plot
plt.show()
sel_df['event'].unique()
# Creating the football field
fig, ax = create_football_field()
# Plotitng the home team
home_team.query('event == "ball_snap"').plot(x='x', y='y', kind='scatter', ax=ax, color='blue', s=20, zorder=2)
# Plotting the away team
away_team.query('event == "ball_snap"').plot(x='x', y='y', kind='scatter', ax=ax, color='orange', s=20, zorder=2)
# Plotting the football
football.query('event == "ball_snap"').plot(x='x', y='y', kind='scatter', ax=ax, color='brown', s=20, zorder=2)
# Displaying the plot
plt.show()