【python】Visualization of field data(NBA) |
您所在的位置:网站首页 › nba球星绘画 › 【python】Visualization of field data(NBA) |
文章目录
1 画篮球场2 爬数据3 可视化投篮结果4 Kobe Bryant5 Others
代码来源: 用Python把NBA球员投篮数据可视化 虎扑热帖|数据分析|用python展示NBA球员出手喜好 1 画篮球场 from matplotlib import pyplot as plt from matplotlib.patches import Arc, Circle, Rectangle %matplotlib inline def draw_ball_field(color='#20458C', lw=2): """ 绘制篮球场 """ # 新建一个大小为(6,6)的绘图窗口 plt.figure(figsize=(9, 9)) # 获得当前的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 ax调用一下 axs = 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()调用一下 years = [2017, 2019]# 球员职业生涯时间 player_id = '201939'# 球员ID shot_data(years,player_id)201939 是 Stephen Curry 调用一下(只用了17,19年的数据) draw_shot('201939')
Kobe Bryant 是 977,球员 ID 通过 https://stats.nba.com/players/ 可以知道,比如 https://stats.nba.com/player/977/ 就是对应着科比的介绍! 来感受下进攻万花筒,有很多负角度的 1)James Harden # 爬数据 years = [2017, 2019]# 球员职业生涯时间 player_id = '201935'# 球员ID shot_data(years,player_id) # 可视化投篮图 draw_shot(player_id)
2)Lebron James # 爬数据 years = [2017, 2019]# 球员职业生涯时间 player_id = '2544'# 球员ID shot_data(years,player_id) # 可视化投篮图 draw_shot(player_id)
4) Kevin Durant 我还是非常喜欢阿杜的,哈哈,早些年 Kobe 带队打世界级的比赛时,总把阿杜和龙王波什弄混了,哈哈哈,一样的…… 阿杜在勇士的 2 个FMVP没得说,配得上“死神”的称号!只希望伤病不会对阿杜造成太大影响,希望他能在球场上多闪耀几个年华!(中距离跳投无解) 5)Joel Embiid 6)Ben Simmons 来看看西帝
7)Clint Capela 8)Draymond Green |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |