# Initialize our new list
player_moments = []
for moment in moments:
# For each player/ball in the list found within each moment
for player in moment[5]:
# Add additional information to each player/ball
# This info includes the index of each moment, the game clock
# and shot clock values for each moment
player.extend((moments.index(moment), moment[2], moment[3]))
player_moments.append(player)

plt.figure(figsize=(15, 11.5))
# Plot the movemnts as scatter plot
# using a colormap to show change in game clock
plt.scatter(harden.x_loc, harden.y_loc, c=harden.game_clock,
cmap=plt.cm.Blues, s=1000, zorder=1)
# Darker colors represent moments earlier on in the game
cbar = plt.colorbar(orientation="horizontal")
cbar.ax.invert_xaxis()
# This plots the court
# zorder=0 sets the court lines underneath Harden's movements
# extent sets the x and y axis values to plot the image within.
# The original animation plots in the SVG coordinate space
# which has x=0, and y=0 at the top left.
# So, we set the axis values the same way in this plot.
# In the list we pass to extent 0,94 representing the x-axis
# values and 50,0 representing the y-axis values
plt.imshow(court, zorder=0, extent=[0,94,50,0])
# extend the x-values beyond the court b/c Harden
# goes out of bounds
plt.xlim(0,101)
plt.show()

plt.figure(figsize=(15, 11.5))
# Plot the movemnts as scatter plot
# using a colormap to show change in game clock
plt.scatter(harden.x_loc, -harden.y_loc, c=harden.game_clock,
cmap=plt.cm.Blues, s=1000, zorder=1)
# Darker colors represent moments earlier on in the game
cbar = plt.colorbar(orientation="horizontal")
# invert the colorbar to have higher numbers on the left
cbar.ax.invert_xaxis()
draw_court()
plt.xlim(0, 101)
plt.ylim(-50, 0)
plt.show()

计算移动距离

def travel_dist(player_locations):
# get the differences for each column
diff = np.diff(player_locations, axis=0)
# square the differences and add them,
# then get the square root of that sum
dist = np.sqrt((diff ** 2).sum(axis=1))
# Then return the sum of all the distances
return dist.sum()

计算平均速度

计算一个球员的平均速度非常简单。我们只需以时间来划分距离。

In [29]:

Python

# get the number of seconds for the play
seconds = df.game_clock.max() - df.game_clock.min()
# feet per second
harden_fps = dist / seconds
# convert to miles per hour
harden_mph = 0.681818 * harden_fps
harden_mph