前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >科比投篮数据可视化小例子

科比投篮数据可视化小例子

作者头像
用户7010445
发布2020-03-03 14:28:59
1K0
发布2020-03-03 14:28:59
举报

作为一名喜欢篮球的python初学者,一直在寻找利用python分析NBA比赛数据的案例,希望可以通过重复这些案例提高自己的python编程水平,毕竟将相对比较无聊的代码和自己的兴趣结合起来,可以使学习过程显得不再那么单调!下文内容介绍自己找到并完全重复其代码的一个案例。

原文地址 How to Create NBA Shot Charts in Python;原文的主要内容是通过可视化的手段展示哈登2014-2015赛季的投篮数据。但是第一部分通过爬虫获取数据的过程自己还不是很理解,没有能够获得原文使用的数据集。稍显遗憾之际想到了自己之前重复过的一个kaggle案例 Kobe Brant Shot Selection —— 科比的投篮选择。数据集完全匹配,遂用科比的投篮数据来重复原文。

第一部分:使用matplotlib画一个篮球场
代码语言:javascript
复制
from matplotlib.patches import Circle
from matplotlib.patches import Rectangle
from matplotlib.patches import Arc

def draw_court(ax=None,color='black',lw=2,outer_lines=False):
    if ax is None:
        ax = plt.gca()
    hoop = Circle((0,0),radius=7.5,linewidth=lw,color=color,fill=False)
    backboard = Rectangle((-30,-7.5),60,-1,linewidth=lw,color=color)
    outer_box = Rectangle((-80,-47.5),160,190,linewidth=lw,color=color,fill=False)
    inner_box = Rectangle((-60,-47.5),120,190,linewidth=lw,color=color,fill=False)
    top_free_throw = Arc((0,142.5),120,120,theta1=0,theta2=180,linewidth=lw,color=color,fill=False)
    bottom_free_throw = Arc((0,142.5),120,120,theta1=180,theta2=0,linewidth=lw,color=color,linestyle='dashed')
    restricted = Arc((0,0),80,80,theta1=0,theta2=180,linewidth=lw,color=color)
    corner_three_a = Rectangle((-220,-47.5),0,140,linewidth=lw,color=color)
    corner_three_b = Rectangle((220,-47.5),0,140,linewidth=lw,color=color)
    three_arc = Arc((0,0),475,475,theta1=22,theta2=158,linewidth=lw,color=color)
    center_outer_arc = Arc((0,422.5),120,120,theta1=180,theta2=0,linewidth=lw,color=color)
    center_inner_arc = Arc((0,422.5),40,40,theta1=180,theta2=0,linewidth=lw,color=color)
    court_elements = [hoop, backboard,outer_box,inner_box,top_free_throw,bottom_free_throw,restricted,corner_three_a,corner_three_b,three_arc,center_outer_arc,center_inner_arc]
    if outer_lines:
        outer_lines = Rectangle((-250,-47.5),500,470,linewidth=lw,color=color,fill=False)
        court_elements.append(outer_lines)
    for element in court_elements:
        ax.add_patch(element)
    return ax

Court_1.jpg

第二部分:篮球场结合科比的投篮数据
代码语言:javascript
复制
import pandas as pd
import matplotlib.pyplot as plt

shot_df = pd.read_csv("data.csv")

plt.figure(figsize=(12,11))
plt.scatter(shot_df.loc_x,shot_df.loc_y)
draw_court(outer_lines=True)
plt.xlim(300,-300)
plt.show()

Court_2.jpg

第三部分:jointplot

jointplot是什么意思自己还不太明白

代码语言:javascript
复制
import seaborn as sns
joint_shot_chart = sns.jointplot(shot_df.loc_x,shot_df.loc_y,stat_func=None,kind='scatter',space=0,alpha=0.5)
joint_shot_chart.fig.set_size_inches(12,11)
ax = joint_shot_chart.ax_joint
draw_court(ax)
ax.set_xlim(-250,250)
ax.set_ylim(422.5,-47.5)
ax.set_xlabel('')
ax.set_ylabel('')
ax.tick_params(labelbottom='off',labelleft='off')
ax.set_title('The location and circumstances of every field goal attempted by Kobe Bryant',y=1.2,fontsize=18)
ax.text(-250,445,'Data Source: Kaggle\nAuthor:MingYan',fontsize=12)
plt.show()

Court_3.jpg

第四部分:抓取一张科比的大头贴
代码语言:javascript
复制
import urllib.request

url = "https://d2cwpp38twqe55.cloudfront.net/req/201902151/images/players/bryanko01.jpg"
picture = urllib.request.urlretrieve(url)
kobe_picture = plt.imread(picture[0])
plt.imshow(kobe_picture)
plt.savefig("Kobe.jpg")

Kobe.jpg

第五部分:将大头贴和投篮分布图结合到一起
代码语言:javascript
复制
from matplotlib.offsetbox import OffsetImage
cmap = plt.cm.YlOrRd_r
joint_shot_chart = sns.jointplot(shot_df.loc_x,shot_df.loc_y,stat_func=None,kind='kde',space=0,color=cmap(0.1),cmap=cmap,n_levels=50)
joint_shot_chart.fig.set_size_inches(12,11)
ax = joint_shot_chart.ax_joint
draw_court(ax)
ax.set_xlim(-250,250)
ax.set_ylim(422.5,-47.5)
ax.set_xlabel('')
ax.set_ylabel('')
ax.tick_params(labelbottom='off',labelleft='off')
ax.set_title('The location of every goal attempted by Kobe Bryabt took during his 20-year career',y=1.2,fontsize=18)
ax.text(-250,445,"Data Source: Kaggle\nPorter: MingYan",fontsize=12)
img = OffsetImage(kobe_picture,zoom=0.6)
img.set_offset((1000,920))
ax.add_artist(img)
plt.savefig("Court_4.jpg")

Court_4.jpg

第五部分:换另外一种风格
代码语言:javascript
复制
cmap = plt.cm.gist_heat_r
joint_shot_chart = sns.jointplot(shot_df.loc_x,shot_df.loc_y,stat_func=None,kind='hex',space=0,color=cmap(.2),cmap=cmap)
joint_shot_chart.fig.set_size_inches(12,11)
ax = joint_shot_chart.ax_joint
draw_court(ax)
ax.set_xlim(-250,250)
ax.set_ylim(422.5,-47.5)
ax.set_xlabel('')
ax.set_ylabel('')
ax.tick_params(labelbottom='off',labelleft='off')
ax.set_title('The location of every goal attempted by Kobe Bryabt took during his 20-year career',y=1.2,fontsize=18)
ax.text(-250,445,"Data Source: Kaggle\nPorter: MingYan",fontsize=12)
img = OffsetImage(kobe_picture,zoom=0.6)
img.set_offset((1000,920))
ax.add_artist(img)
plt.savefig("Court_5.jpg")

Court_5.jpg

第六部分:版本信息
代码语言:javascript
复制
print('Python version:',sys.version_info)
import IPython
print('IPython version:', IPython.__version__)
print('Urllib.requests version:', urllib.request.__version__)
import matplotlib as mpl
print('Matplotlib version:', mpl.__version__)
print('Seaborn version:', sns.__version__)
print('Pandas version:', pd.__version__)
代码语言:javascript
复制
Python version: sys.version_info(major=3, minor=6, micro=3, releaselevel='final', serial=0)
IPython version: 6.1.0
Urllib.requests version: 3.6
Matplotlib version: 2.1.0
Seaborn version: 0.5.0
Pandas version: 0.20.3
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-04-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第一部分:使用matplotlib画一个篮球场
  • 第二部分:篮球场结合科比的投篮数据
  • 第三部分:jointplot
  • 第四部分:抓取一张科比的大头贴
  • 第五部分:将大头贴和投篮分布图结合到一起
  • 第五部分:换另外一种风格
  • 第六部分:版本信息
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档