sportowe_wizualizacja/Through_Ball.ipynb

403 lines
43 KiB
Plaintext
Raw Normal View History

2019-06-03 13:31:10 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"done\n"
]
}
],
"source": [
"import json\n",
"import os\n",
"from pandas.io.json import json_normalize\n",
"import numpy as np\n",
"import seaborn as sns\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib.patches import Arc, Rectangle, ConnectionPatch\n",
"from matplotlib.offsetbox import OffsetImage\n",
"#import squarify\n",
"from functools import reduce\n",
"path = \"\"\"C:\\\\Users\\\\Koushik\\\\Downloads\\\\open-data-master\\\\open-data-master\\\\data\\\\my_events\\\\\"\"\"\n",
"Xg_req = pd.DataFrame(data=None)\n",
"for filename in (os.listdir(path)):\n",
" #print(filename)\n",
" \n",
" with open(\"%s\" % path + filename,encoding=\"utf8\") as data_file: \n",
" data = json.load(data_file)\n",
" df = pd.DataFrame(data=None)\n",
" \n",
" df = json_normalize(data, sep = \"_\")\n",
" \n",
" #df = df[(df['type_name'] == \"Shot\")]\n",
" #df = df.loc[:,['location','shot_body_part_id','shot_end_location','shot_one_on_one','shot_technique_id','shot_type_id','under_pressure','shot_outcome_id']]\n",
" #print(df.shape)\n",
" Xg_req = Xg_req.append(df,ignore_index=True,sort=False)\n",
" #df.drop(df.index, inplace=True)\n",
" \n",
"print(\"done\")\n",
"df = Xg_req"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"through_ball = df.query('pass_through_ball == True')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"118\n",
"12\n"
]
}
],
"source": [
"assist = df.query('shot_follows_dribble == True')\n",
"print(len(assist.index))\n",
"goal = assist.query('shot_outcome_id == 97')\n",
"#assist = df.query('pass_goal_assist == True')\n",
"print(len(goal.index))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id</th>\n",
" <th>period</th>\n",
" <th>timestamp</th>\n",
" <th>location</th>\n",
" <th>pass_end_location</th>\n",
" <th>pass_recipient_name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>95ffe924-77f3-40e2-afc6-23e5fed8865f</td>\n",
" <td>1</td>\n",
" <td>00:00:38.660</td>\n",
" <td>[44.0, 17.0]</td>\n",
" <td>[95.0, 23.0]</td>\n",
" <td>Francesca Kirby</td>\n",
" </tr>\n",
" <tr>\n",
" <th>257</th>\n",
" <td>ce817472-51c1-4b9a-89d5-2b929f422b2e</td>\n",
" <td>1</td>\n",
" <td>00:08:03.753</td>\n",
" <td>[89.0, 65.0]</td>\n",
" <td>[116.0, 55.0]</td>\n",
" <td>Nikita Parris</td>\n",
" </tr>\n",
" <tr>\n",
" <th>623</th>\n",
" <td>cf2ed5ec-1d2f-4746-a482-4ff7a1823adf</td>\n",
" <td>1</td>\n",
" <td>00:19:30.020</td>\n",
" <td>[39.0, 38.0]</td>\n",
" <td>[96.0, 42.0]</td>\n",
" <td>Ramona Bachmann</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1574</th>\n",
" <td>898b1dba-d2e6-431c-b830-4d897c946ad2</td>\n",
" <td>2</td>\n",
" <td>00:01:56.558</td>\n",
" <td>[41.0, 79.0]</td>\n",
" <td>[101.0, 47.0]</td>\n",
" <td>Ramona Bachmann</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3292</th>\n",
" <td>7799fb29-209f-4b3a-a73e-2c35f9d83c51</td>\n",
" <td>1</td>\n",
" <td>00:09:35.360</td>\n",
" <td>[74.0, 48.0]</td>\n",
" <td>[98.0, 32.0]</td>\n",
" <td>Mallory Weber</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" id period timestamp \\\n",
"27 95ffe924-77f3-40e2-afc6-23e5fed8865f 1 00:00:38.660 \n",
"257 ce817472-51c1-4b9a-89d5-2b929f422b2e 1 00:08:03.753 \n",
"623 cf2ed5ec-1d2f-4746-a482-4ff7a1823adf 1 00:19:30.020 \n",
"1574 898b1dba-d2e6-431c-b830-4d897c946ad2 2 00:01:56.558 \n",
"3292 7799fb29-209f-4b3a-a73e-2c35f9d83c51 1 00:09:35.360 \n",
"\n",
" location pass_end_location pass_recipient_name \n",
"27 [44.0, 17.0] [95.0, 23.0] Francesca Kirby \n",
"257 [89.0, 65.0] [116.0, 55.0] Nikita Parris \n",
"623 [39.0, 38.0] [96.0, 42.0] Ramona Bachmann \n",
"1574 [41.0, 79.0] [101.0, 47.0] Ramona Bachmann \n",
"3292 [74.0, 48.0] [98.0, 32.0] Mallory Weber "
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"through_ball= through_ball[[\"id\", \"period\", \"timestamp\", \"location\", \"pass_end_location\", \"pass_recipient_name\"]]\n",
"through_ball.head()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id</th>\n",
" <th>period</th>\n",
" <th>timestamp</th>\n",
" <th>location</th>\n",
" <th>pass_end_location</th>\n",
" <th>pass_recipient_name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>199</th>\n",
" <td>c547c736-4bf5-484f-9360-463abb19e465</td>\n",
" <td>1</td>\n",
" <td>00:05:45.020</td>\n",
" <td>[104.0, 36.0]</td>\n",
" <td>[109.0, 29.0]</td>\n",
" <td>Millie Bright</td>\n",
" </tr>\n",
" <tr>\n",
" <th>746</th>\n",
" <td>e3eca08d-7c54-4c65-a19a-faa9708225b5</td>\n",
" <td>1</td>\n",
" <td>00:23:20.340</td>\n",
" <td>[111.0, 24.0]</td>\n",
" <td>[111.0, 35.0]</td>\n",
" <td>So-yun Ji</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1649</th>\n",
" <td>d48d70cf-3464-40c9-80c0-f81d7ce46b25</td>\n",
" <td>2</td>\n",
" <td>00:03:39.570</td>\n",
" <td>[108.0, 5.0]</td>\n",
" <td>[115.0, 43.0]</td>\n",
" <td>Nikita Parris</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2739</th>\n",
" <td>7f1fe749-1914-4f5d-8f0b-62c7c9f79196</td>\n",
" <td>2</td>\n",
" <td>00:40:33.251</td>\n",
" <td>[109.0, 48.0]</td>\n",
" <td>[97.0, 46.0]</td>\n",
" <td>Georgia Stanway</td>\n",
" </tr>\n",
" <tr>\n",
" <th>63663</th>\n",
" <td>59c55828-1423-4cf5-aef4-58f0cab1d9fe</td>\n",
" <td>2</td>\n",
" <td>00:06:57.202</td>\n",
" <td>[81.0, 28.0]</td>\n",
" <td>[87.0, 36.0]</td>\n",
" <td>Hayley Raso</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" id period timestamp \\\n",
"199 c547c736-4bf5-484f-9360-463abb19e465 1 00:05:45.020 \n",
"746 e3eca08d-7c54-4c65-a19a-faa9708225b5 1 00:23:20.340 \n",
"1649 d48d70cf-3464-40c9-80c0-f81d7ce46b25 2 00:03:39.570 \n",
"2739 7f1fe749-1914-4f5d-8f0b-62c7c9f79196 2 00:40:33.251 \n",
"63663 59c55828-1423-4cf5-aef4-58f0cab1d9fe 2 00:06:57.202 \n",
"\n",
" location pass_end_location pass_recipient_name \n",
"199 [104.0, 36.0] [109.0, 29.0] Millie Bright \n",
"746 [111.0, 24.0] [111.0, 35.0] So-yun Ji \n",
"1649 [108.0, 5.0] [115.0, 43.0] Nikita Parris \n",
"2739 [109.0, 48.0] [97.0, 46.0] Georgia Stanway \n",
"63663 [81.0, 28.0] [87.0, 36.0] Hayley Raso "
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"assist= assist[[\"id\", \"period\", \"timestamp\", \"location\", \"pass_end_location\", \"pass_recipient_name\"]]\n",
"assist.head()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def draw_pitch(ax):\n",
" # focus on only half of the pitch\n",
" #Pitch Outline & Centre Line\n",
" Pitch = Rectangle([0,0], width = 120, height = 80, fill = False)\n",
" #Left, Right Penalty Area and midline\n",
" LeftPenalty = Rectangle([0,22.3], width = 14.6, height = 35.3, fill = False)\n",
" RightPenalty = Rectangle([105.4,22.3], width = 14.6, height = 35.3, fill = False)\n",
" midline = ConnectionPatch([60,0], [60,80], \"data\", \"data\")\n",
"\n",
" #Left, Right 6-yard Box\n",
" LeftSixYard = Rectangle([0,32], width = 4.9, height = 16, fill = False)\n",
" RightSixYard = Rectangle([115.1,32], width = 4.9, height = 16, fill = False)\n",
"\n",
"\n",
" #Prepare Circles\n",
" centreCircle = plt.Circle((60,40),8.1,color=\"black\", fill = False)\n",
" centreSpot = plt.Circle((60,40),0.71,color=\"black\")\n",
" #Penalty spots and Arcs around penalty boxes\n",
" leftPenSpot = plt.Circle((9.7,40),0.71,color=\"black\")\n",
" rightPenSpot = plt.Circle((110.3,40),0.71,color=\"black\")\n",
" leftArc = Arc((9.7,40),height=16.2,width=16.2,angle=0,theta1=310,theta2=50,color=\"black\")\n",
" rightArc = Arc((110.3,40),height=16.2,width=16.2,angle=0,theta1=130,theta2=230,color=\"black\")\n",
" \n",
" element = [Pitch, LeftPenalty, RightPenalty, midline, LeftSixYard, RightSixYard, centreCircle, \n",
" centreSpot, rightPenSpot, leftPenSpot, leftArc, rightArc]\n",
" for i in element:\n",
" ax.add_patch(i)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAawAAAEyCAYAAACmpOSfAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzsnXd4k2UXxu+nlNGC7A0CsgSsyCirCMoUUAFFhiCioixlKoqg4gJBRJxM2QIyZYkiQ0AUkJYtIBQEGZVV2bRAe74/7uZr2qbtmyZp0nJ+15Ur6x0nobx3znnOMCICRVEURfF1/LxtgKIoiqJYQQVLURRFyRCoYCmKoigZAhUsRVEUJUOggqUoiqJkCFSwFEVRlAyBCpaiKIqSIVDBUhRFUTIEKliKoihKhsA/PU9WsGBBKVOmTHqeUlEURfFhwsLCzotIISvbpqtglSlTBqGhoel5SkVRFMWHMcYct7qthgQVRVGUDIElwTLGDDTG/GmM2WeMmWeMyWGMuccYs80Yc9gYM98Yk83TxiqKoih3LqkKljGmBIB+AIJFJAhAFgCdAIwGME5EKgD4D0B3TxqqKIqi3NlYDQn6AwgwxvgDCAQQAaAxgEVx788E0Nb95imKoigKSTXpQkROGWM+AfAPgBsAfgYQBuCiiNyO2+wkgBKO9jfG9ADQAwBKlSrl8BxlypTB8eOW190URVGUDEbp0qVx7Ngxl46RqmAZY/IBaAPgHgAXASwE0NLBpg4nQYrIZACTASA4ONjhNsePH4cOklTuFGbMmIENGzZgxow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x360 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"fig=plt.figure() #set up the figures\n",
"fig.set_size_inches(7, 5)\n",
"ax=fig.add_subplot(1,1,1)\n",
"draw_pitch(ax) #overlay our different objects on the pitch\n",
"plt.ylim(-2, 82)\n",
"plt.xlim(-2, 122)\n",
"#plt.plot(x_axis,y_axis,'ro')\n",
"#plt.plot(x,y,'bo')\n",
"#plt.axis('off')\n",
"\n",
"\"\"\"for i in range(len(through_ball)):\n",
" # annotate draw an arrow from a current position to pass_end_location\n",
" ax.annotate(\"\", xy = (through_ball.iloc[i]['pass_end_location'][0], through_ball.iloc[i]['pass_end_location'][1]), xycoords = 'data',\n",
" xytext = (through_ball.iloc[i]['location'][0], through_ball.iloc[i]['location'][1]), textcoords = 'data',\n",
" arrowprops=dict(arrowstyle=\"->\",connectionstyle=\"arc3\", color = \"blue\"),)\n",
"\"\"\"\n",
"for i in range(len(assist)):\n",
" # annotate draw an arrow from a current position to pass_end_location\n",
" ax.annotate(\"\", xy = (assist.iloc[i]['pass_end_location'][0], assist.iloc[i]['pass_end_location'][1]), xycoords = 'data',\n",
" xytext = (assist.iloc[i]['location'][0], assist.iloc[i]['location'][1]), textcoords = 'data',\n",
" arrowprops=dict(arrowstyle=\"->\",connectionstyle=\"arc3\", color = \"red\"),)\n",
"\n",
"\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}