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

Posted 红皇后学术

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了科比投篮数据可视化小例子相关的知识,希望对你有一定的参考价值。

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

第一部分:使用matplotlib画一个篮球场
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

第二部分:篮球场结合科比的投篮数据

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是什么意思自己还不太明白

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

第四部分:抓取一张科比的大头贴

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

第五部分:将大头贴和投篮分布图结合到一起

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

第五部分:换另外一种风格

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

第六部分:版本信息

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__)
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

扩展阅读



以上是关于科比投篮数据可视化小例子的主要内容,如果未能解决你的问题,请参考以下文章

科比,老大1000天

kaggle科比投篮数据集的画图案例

NBA球员投篮数据可视化

NBA 可视化使用Pyecharts实现湖人19-20赛季投篮数据可视化~

科比职业生涯场均得分~django+chartjs数据可视化柱形图简单

☀️通过antd-charts可视化对比科比和詹姆斯谁更强☀️