NBA球员投篮数据可视化
Posted 顶级程序员
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NBA球员投篮数据可视化相关的知识,希望对你有一定的参考价值。
源 / 法纳斯特 文 / 小F
from matplotlib import pyplot as plt
from matplotlib.patches import Arc, Circle, Rectangle
def draw_ball_field(color='#20458C', lw=2):
"""
绘制篮球场
"""
# 新建一个大小为(6,6)的绘图窗口
plt.figure(figsize=(6, 6))
# 获得当前的Axes对象ax,进行绘图
ax = plt.gca()
# 对篮球场进行底色填充
lines_outer_rec = Rectangle(xy=(-250, -47.5), width=500, height=470, linewidth=lw, color='#F0F0F0', fill=True)
# 设置篮球场填充图层为最底层
lines_outer_rec.set_zorder(0)
# 将rec添加进ax
ax.add_patch(lines_outer_rec)
# 绘制篮筐,半径为7.5
circle_ball = Circle(xy=(0, 0), radius=7.5, linewidth=lw, color=color, fill=False)
# 将circle添加进ax
ax.add_patch(circle_ball)
# 绘制篮板,尺寸为(60,1)
plate = Rectangle(xy=(-30, -7.5), width=60, height=-1, linewidth=lw, color=color, fill=False)
# 将rec添加进ax
ax.add_patch(plate)
# 绘制2分区的外框线,尺寸为(160,190)
outer_rec = Rectangle(xy=(-80, -47.5), width=160, height=190, linewidth=lw, color=color, fill=False)
# 将rec添加进ax
ax.add_patch(outer_rec)
# 绘制2分区的内框线,尺寸为(120,190)
inner_rec = Rectangle(xy=(-60, -47.5), width=120, height=190, linewidth=lw, color=color, fill=False)
# 将rec添加进ax
ax.add_patch(inner_rec)
# 绘制罚球区域圆圈,半径为60
circle_punish = Circle(xy=(0, 142.5), radius=60, linewidth=lw, color=color, fill=False)
# 将circle添加进ax
ax.add_patch(circle_punish)
# 绘制三分线的左边线
three_left_rec = Rectangle(xy=(-220, -47.5), width=0, height=140, linewidth=lw, color=color, fill=False)
# 将rec添加进ax
ax.add_patch(three_left_rec)
# 绘制三分线的右边线
three_right_rec = Rectangle(xy=(220, -47.5), width=0, height=140, linewidth=lw, color=color, fill=False)
# 将rec添加进ax
ax.add_patch(three_right_rec)
# 绘制三分线的圆弧,圆心为(0,0),半径为238.66,起始角度为22.8,结束角度为157.2
three_arc = Arc(xy=(0, 0), width=477.32, height=477.32, theta1=22.8, theta2=157.2, linewidth=lw, color=color, fill=False)
# 将arc添加进ax
ax.add_patch(three_arc)
# 绘制中场处的外半圆,半径为60
center_outer_arc = Arc(xy=(0, 422.5), width=120, height=120, theta1=180, theta2=0, linewidth=lw, color=color, fill=False)
# 将arc添加进ax
ax.add_patch(center_outer_arc)
# 绘制中场处的内半圆,半径为20
center_inner_arc = Arc(xy=(0, 422.5), width=40, height=40, theta1=180, theta2=0, linewidth=lw, color=color, fill=False)
# 将arc添加进ax
ax.add_patch(center_inner_arc)
# 绘制篮球场外框线,尺寸为(500,470)
lines_outer_rec = Rectangle(xy=(-250, -47.5), width=500, height=470, linewidth=lw, color=color, fill=False)
# 将rec添加进ax
ax.add_patch(lines_outer_rec)
return axaxs = draw_ball_field(color='#20458C', lw=2)
# 设置坐标轴范围
axs.set_xlim(-250, 250)
axs.set_ylim(422.5, -47.5)
# 消除坐标轴刻度
axs.set_xticks([])
axs.set_yticks([])
# 添加备注信息
plt.annotate('By xiao F', xy=(100, 160), xytext=(178, 418))
plt.show()
import requests
import json
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
# 球员职业生涯时间
years = [2018, 2019]
for i in range(years[0], years[1]):
# 赛季
season = str(i) + '-' + str(i + 1)[-2:]
# 球员ID
player_id = '201939'
# 请求网址
url = 'https://stats.nba.com/stats/shotchartdetail?AheadBehind=&CFID=33&CFPARAMS=' + season + '&ClutchTime=&Conference=&ContextFilter=&ContextMeasure=FGA&DateFrom=&DateTo=&Division=&EndPeriod=10&EndRange=28800&GROUP_ID=&GameEventID=&GameID=&GameSegment=&GroupID=&GroupMode=&GroupQuantity=5&LastNGames=0&LeagueID=00&Location=&Month=0&OnOff=&OpponentTeamID=0&Outcome=&PORound=0&Period=0&PlayerID=' + player_id + '&PlayerID1=&PlayerID2=&PlayerID3=&PlayerID4=&PlayerID5=&PlayerPosition=&PointDiff=&Position=&RangeType=0&RookieYear=&Season=' + season + '&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&StartPeriod=1&StartRange=0&StarterBench=&TeamID=0&VsConference=&VsDivision=&VsPlayerID1=&VsPlayerID2=&VsPlayerID3=&VsPlayerID4=&VsPlayerID5=&VsTeamID='
# 请求结果
response = requests.get(url=url, headers=headers)
result = json.loads(response.text)
# 获取数据
for item in result['resultSets'][0]['rowSet']:
# 是否进球得分
flag = item[10]
# 横坐标
loc_x = str(item[17])
# 纵坐标
loc_y = str(item[18])
with open('curry.csv', 'a+') as f:
f.write(loc_x + ',' + loc_y + ',' + flag + '\n')
import pandas as pd
# 读取数据
df = pd.read_csv('curry.csv', header=None, names=['width', 'height', 'type'])
# 分类数据
df1 = df[df['type'] == 'Made Shot']
df2 = df[df['type'] == 'Missed Shot']
# 绘制散点图
axs.scatter(x=df2['width'], y=df2['height'], s=30, marker='x', color='#A82B2B')
axs.scatter(x=df1['width'], y=df1['height'], s=30, marker='o', edgecolors='#3A7711', color="#F0F0F0", linewidths=2)
import seaborn as sns
import matplotlib as mpl
# 读取数据
df = pd.read_csv('curry.csv', header=None, names=['width', 'height', 'type'])
def colormap():
"""
颜色转换
"""
return mpl.colors.LinearSegmentedColormap.from_list('cmap', ['#C5C5C5', '#9F9F9F', '#706A7C', '#675678', '#713A71','#9D3E5E', '#BC5245', '#C86138', '#C96239', '#D37636', '#D67F39', '#DA8C3E', '#E1A352'], 256)
# 绘制球员投篮热力图
shot_heatmap = sns.jointplot(df['width'], df['height'], stat_func=None, kind='kde', space=0, color='w', cmap=colormap())
# 设置图像大小
shot_heatmap.fig.set_size_inches(6, 6)
# 图像反向
ax = shot_heatmap.ax_joint
# 绘制投篮散点图
ax.scatter(x=df['width'], y=df['height'], s=0.1, marker='o', color="w", alpha=1)
# 添加篮球场
draw_ball_field(color='w', lw=2)
# 将坐标轴颜色更改为白色
lines = plt.gca()
lines.spines['top'].set_color('none')
lines.spines['left'].set_color('none')
# 去除坐标轴标签
ax.axis('off')
-END-
转载声明:本文选自「法纳斯特」。
重磅推出全新学习模式
用打卡学Python
每天30分钟
30天学会Python编程
世界正在奖励坚持学习的人!
以上是关于NBA球员投篮数据可视化的主要内容,如果未能解决你的问题,请参考以下文章
NBA 可视化使用Pyecharts实现湖人19-20赛季投篮数据可视化~