用 Python 创建 NBA 得分图表
admin
2023-07-30 22:43:30
0

在这篇文章中,我研究了如何提取一个篮球运动员的得分图数据,然后使用 matplotlibseaborn 绘制得分图。

123456 %matplotlib inline import requestsimport matplotlib.pyplot as pltimport pandas as pdimport seaborn as sns

获取数据

从 stats.nba.com 获取数据是非常简单的。虽然 NBA 没有提供一个公共的 API,但是实际上我们可以通过使用 requests 库来访问 NBA 用在 stats.nba.com 上的 API。Greg Reda 发布的这篇博客很好地解释了如何访问这个 API(或者为任意 web 应用找到一个 API 来完成上述问题)。

我们将会使用这个 URL 来获取 James Harden 的得分图数据。

123456789 shot_chart_url = \’http://stats.nba.com/stats/shotchartdetail?CFID=33&CFPAR\’\\                \’AMS=2014-15&ContextFilter=&ContextMeasure=FGA&DateFrom=&D\’\\                \’ateTo=&GameID=&GameSegment=&LastNGames=0&LeagueID=00&Loca\’\\                \’tion=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&\’\\                \’PaceAdjust=N&PerMode=PerGame&Period=0&PlayerID=201935&Plu\’\\                \’sMinus=N&Position=&Rank=N&RookieYear=&Season=2014-15&Seas\’\\                \’onSegment=&SeasonType=Regular+Season&TeamID=0&VsConferenc\’\\                \’e=&VsDivision=&mode=Advanced&showDetails=0&showShots=1&sh\’\\                \’owZones=0\’

上述 URL 发送给我们一个 JSON 文件,该文件包含我们想要的数据。也要注意到 URL 包含了用于访问数据的各种 API 参数。URL 中被设置为 201935 的 PlayerID 参数就是 James Harden 的 PlayerID

现在让我们使用 requests 库来获取我们想要的数据。

123456 # Get the webpage containing the dataresponse = requests.get(shot_chart_url)# Grab the headers to be used as column headers for our DataFrameheaders = response.json()[\’resultSets\’][0][\’headers\’]# Grab the shot chart datashots = response.json()[\’resultSets\’][0][\’rowSet\’]

利用获取到的得分图数据创建一个 pandas DataFrame

12345 shot_df = pd.DataFrame(shots, columns=headers)# View the head of the DataFrame and all its columnsfrom IPython.display import displaywith pd.option_context(\’display.max_columns\’, None):    display(shot_df.head())

 

上述的得分图数据包含了 2014-15 常规赛期间 James Harden 所有的投篮出手次数。我们想要的数据在 LOC_XLOC_Y 中。这是每次投篮出手位置的坐标值,然后就可以在代表篮球场的一组坐标轴上绘制这些坐标值了。

绘制得分图数据

让我们快速地绘制数据,看看数据是如何分布的。

12345 sns.set_style(\”white\”)sns.set_color_codes()plt.figure(figsize=(12,11))plt.scatter(shot_df.LOC_X, shot_df.LOC_Y)plt.show()

注意到上述的图表没能很好的表示数据。横坐标轴上的值与实际的相反了。下面只把右边的投篮绘制出来,看看问题出在哪里。

123456 right = shot_df[shot_df.SHOT_ZONE_AREA == \”Right Side(R)\”]plt.figure(figsize=(12,11))plt.scatter(right.LOC_X, right.LOC_Y)plt.xlim(300,300)plt.ylim(100,500)pan class=\”crayon-sy\”>)plt.ylim(100,500)文章中,我研究了如何提取一个篮球运动员的得分图数据,然后使用 matplotlibseaborn 绘制得分图。

123456 %matplotlib inline import requestsimport matplotlib.pyplot as pltimport pandas as pdimport seaborn as sns

获取数据

从 stats.nba.com 获取数据是非常简单的。虽然 NBA 没有提供一个公共的 API,但是实际上我们可以通过使用 requests 库来访问 NBA 用在 stats.nba.com 上的 API。Greg Reda 发布的这篇博客很好地解释了如何访问这个 API(或者为任意 web 应用找到一个 API 来完成上述问题)。

我们将会使用这个 URL 来获取 James Harden 的得分图数据。

123456789 shot_chart_url = \’http://stats.nba.com/stats/shotchartdetail?CFID=33&CFPAR\’\\                \’AMS=2014-15&ContextFilter=&ContextMeasure=FGA&DateFrom=&D\’\\                \’ateTo=&GameID=&GameSegment=&LastNGames=0&LeagueID=00&Loca\’\\                \’tion=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&\’\\                \’PaceAdjust=N&PerMode=PerGame&Period=0&PlayerID=201935&Plu\’\\                \’sMinus=N&Position=&Rank=N&RookieYear=&Season=2014-15&Seas\’\\                \’onSegment=&SeasonType=Regular+Season&TeamID=0&VsConferenc\’\\                \’e=&VsDivision=&mode=Advanced&showDetails=0&showShots=1&sh\’\\                \’owZones=0\’

上述 URL 发送给我们一个 JSON 文件,该文件包含我们想要的数据。也要注意到 URL 包含了用于访问数据的各种 API 参数。URL 中被设置为 201935 的 PlayerID 参数就是 James Harden 的 PlayerID

现在让我们使用 requests 库来获取我们想要的数据。

123456 # Get the webpage containing the dataresponse = requests.get(shot_chart_url)# Grab the headers to be used as column headers for our DataFrameheaders = response.json()[\’resultSets\’][0][\’headers\’]# Grab the shot chart datashots = response.json()[\’resultSets\’][0][\’rowSet\’]

利用获取到的得分图数据创建一个 pandas DataFrame

12345 shot_df = pd.DataFrame(shots, columns=headers)# View the head of the DataFrame and all its columnsfrom IPython.display import displaywith pd.option_context(\’display.max_columns\’, None):    display(shot_df.head())

 

上述的得分图数据包含了 2014-15 常规赛期间 James Harden 所有的投篮出手次数。我们想要的数据在 LOC_XLOC_Y 中。这是每次投篮出手位置的坐标值,然后就可以在代表篮球场的一组坐标轴上绘制这些坐标值了。

绘制得分图数据

让我们快速地绘制数据,看看数据是如何分布的。

12345 sns.set_style(\”white\”)sns.set_color_codes()plt.figure(figsize=(12,11))plt.scatter(shot_df.LOC_X, shot_df.LOC_Y)plt.show()

注意到上述的图表没能很好的表示数据。横坐标轴上的值与实际的相反了。下面只把右边的投篮绘制出来,看看问题出在哪里。

123456 right = shot_df[shot_df.SHOT_ZONE_AREA == \”Right Side(R)\”]plt.figure(figsize=(12,11))plt.scatter(right.LOC_X, right.LOC_Y)plt.xlim(300,300)plt.ylim(100

相关内容

热门资讯

500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
python绘图库Matplo... 本文简单介绍了Python绘图库Matplotlib的安装,简介如下: matplotlib是pyt...
Prometheus+Graf... 一,Prometheus概述 1,什么是Prometheus?Prometheus是最初在Sound...