sportowe_wizualizacja/Dribbles by player and season.ipynb

324 lines
51 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#change this cell to change season and player name\n",
"season = '2005/2006'\n",
"ssn = '0506'\n",
"length = int(20)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done\n"
]
}
],
"source": [
"import os\n",
"import json\n",
"from pandas.io.json import json_normalize\n",
"import codecs\n",
"import pandas as pd\n",
"import codecs\n",
"main_df = pd.DataFrame(data=None)\n",
"path_match = \"\"\"C:\\\\Users\\\\koushik.r\\\\Documents\\\\open-data-master\\\\\\\\data\\\\events\\\\\"\"\" #location for play by play events\n",
"for root, dirs, files in os.walk(r'C:\\Users\\koushik.r\\Documents\\open-data-master\\data\\matches'):\n",
" for file in files:\n",
" with open(os.path.join(root, file), \"r\") as auto:\n",
" with codecs.open(root + str('\\\\') + file,encoding='utf-8') as data_file:\n",
" data = json.load(data_file)\n",
" df = pd.DataFrame(data=None)\n",
" df = json_normalize(data, sep = \"_\")\n",
" #for x in df.competition_country_name:\n",
" # if x == 'Spain':\n",
" # print(df.match_id)\n",
" for i in range(len(df)):\n",
" if df.iloc[i]['competition_country_name'] == 'Spain' and df.iloc[i]['season_season_name'] == season :\n",
" match_no = df.iloc[i]['match_id'] #gets match with Spain as country\n",
" match_no = str(match_no) # from int to str \n",
" with codecs.open(path_match + match_no + str(r'.json'),encoding=\"utf8\") as event_file: #open the respective file\n",
" df_match = json.load(event_file)\n",
" df_match2 = pd.DataFrame(data=None)\n",
" df_match2 = json_normalize(df_match,sep=\"_\") \n",
" \n",
" main_df = main_df.append(df_match2,ignore_index=True,sort=False) \n",
"print('Done')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"Player = main_df.query('player_id == 5503 & type_id ==43 & play_pattern_id ==1 & duration >= 1.50')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"player_name = Player.player_name.iloc[0]\n",
"#df[['a','b']]\n",
"Player = Player [['location','carry_end_location']]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from math import sqrt\n",
"distance= []\n",
"for i in range(len(Player)):\n",
" distance.append(sqrt((Player.iloc[i]['carry_end_location'][0] - Player.iloc[i]['location'][0])**2 + ((Player.iloc[i]['carry_end_location'][1] - Player.iloc[i]['location'][1])**2)))\n",
" #using distance formula above (sqrt((x2-x1)^2 + (y2-y1)^2))\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"Player['dribble_distance'] = distance"
]
},
{
"cell_type": "code",
"execution_count": 7,
"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>location</th>\n",
" <th>carry_end_location</th>\n",
" <th>dribble_distance</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2791</th>\n",
" <td>[81.9, 77.4]</td>\n",
" <td>[85.6, 66.6]</td>\n",
" <td>11.416217</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3160</th>\n",
" <td>[78.7, 7.3]</td>\n",
" <td>[115.6, 22.8]</td>\n",
" <td>40.023243</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3765</th>\n",
" <td>[45.5, 4.5]</td>\n",
" <td>[48.2, 6.4]</td>\n",
" <td>3.301515</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4811</th>\n",
" <td>[43.1, 40.0]</td>\n",
" <td>[50.3, 28.4]</td>\n",
" <td>13.652839</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5012</th>\n",
" <td>[77.0, 65.6]</td>\n",
" <td>[97.8, 53.3]</td>\n",
" <td>24.164644</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" location carry_end_location dribble_distance\n",
"2791 [81.9, 77.4] [85.6, 66.6] 11.416217\n",
"3160 [78.7, 7.3] [115.6, 22.8] 40.023243\n",
"3765 [45.5, 4.5] [48.2, 6.4] 3.301515\n",
"4811 [43.1, 40.0] [50.3, 28.4] 13.652839\n",
"5012 [77.0, 65.6] [97.8, 53.3] 24.164644"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Player.head()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"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=\"white\", fill = False)\n",
" centreSpot = plt.Circle((60,40),0.71,color=\"white\")\n",
" #Penalty spots and Arcs around penalty boxes\n",
" leftPenSpot = plt.Circle((9.7,40),0.71,color=\"white\")\n",
" rightPenSpot = plt.Circle((110.3,40),0.71,color=\"white\")\n",
" leftArc = Arc((9.7,40),height=16.2,width=16.2,angle=0,theta1=310,theta2=50,color=\"white\")\n",
" rightArc = Arc((110.3,40),height=16.2,width=16.2,angle=0,theta1=130,theta2=230,color=\"white\")\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": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'plt.rcParams.update({\\n \"lines.color\": \"white\",\\n \"patch.edgecolor\": \"white\",\\n \"text.color\": \"black\",\\n \"axes.facecolor\": \"white\",\\n \"axes.edgecolor\": \"lightgray\",\\n \"axes.labelcolor\": \"white\",\\n \"xtick.color\": \"white\",\\n \"ytick.color\": \"white\",\\n \"grid.color\": \"lightgray\",\\n \"figure.facecolor\": \"black\",\\n \"figure.edgecolor\": \"black\",\\n \"savefig.facecolor\": \"black\",\\n \"savefig.edgecolor\": \"black\"})'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"'''plt.rcParams.update({\n",
" \"lines.color\": \"white\",\n",
" \"patch.edgecolor\": \"white\",\n",
" \"text.color\": \"black\",\n",
" \"axes.facecolor\": \"white\",\n",
" \"axes.edgecolor\": \"lightgray\",\n",
" \"axes.labelcolor\": \"white\",\n",
" \"xtick.color\": \"white\",\n",
" \"ytick.color\": \"white\",\n",
" \"grid.color\": \"lightgray\",\n",
" \"figure.facecolor\": \"black\",\n",
" \"figure.edgecolor\": \"black\",\n",
" \"savefig.facecolor\": \"black\",\n",
" \"savefig.edgecolor\": \"black\"})'''"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA6IAAAIcCAYAAADsTxM2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9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X7884776S0tJQlS5bw9ttvU6tWLQAOOeQQ3n33XRYvXswXX3xR/kuD70ava9euzeuvv06TJk1Yvnw5y5cvp1evXlxzzTWceuqpLF++nPHjxwMwYsQIfvGLX/wgU//+/Vm0aBHTp0/n6KOPLs/3/WUlSdWPjagkaaPy8/MZOnQoxcXFNGrUiIsuuoinn36aPffcs3yZXr16ceONN7L99tszdepUbrnlFgBq167NP/7xDwYNGkSjRo3o1asXDz74IHvttVeltn3iiSeyYsUKnnvuOd58880NXg/YuXNnWrVqxZFHHsn1119P69atAbjhhhsoLCyksLCQbt26bfCMaq9evejevTv169dn3bp1DB06lAkTJtC0aVOOPPJILr30Urp27QrAvffey7333st2221HYWEhzz777CazDxw4sDxvt27dmDhxIrNnzy5/vaJ989hjj3HuuedSr1499t57b/71r38BcPnllzNr1ix23HFHdtppJ6655hpCCJvMss0223DQQQfx/PPPb3K5Tbnzzjv5yU9+Un6m98orr2TdunU0a9aM119/nT//+c/suOOOtG/fvryp/M7KlSs55phjmD17NnXr1qVu3boMHjyYW2+9lWeeeYa6devSvn37DW63U6dOTJkyhYYNG3LHHXfw2GOPbfFnkCQli42oJGmjDjzwQLbddltuu+02Vq9ezYgRI3j11Vfp1atX+TIvvvgi48aNY+3atTz99NPlTcX//d//UVpayuOPP87atWv56KOPeOGFFzjppJMqte0+ffrwzDPPsG7dOgYNGkSvXr3Iz8//wTI33ngjX3/9NR9//DETJkygXbt2AJxyyinccsstLF68mFmzZnHffff9aP333Xcfs2bN4uuvv6Zjx47suOOO3HzzzaxevZqSkhL++te/lp/VXL16dflIa1lZGWPGjNlk9tGjR7PDDjuw55570rt3bwYOHPiD1yvaN6tXr2avvfaibt26LFmyhI8++qj8540bN6Z58+asWbOGd955p8L9uP3225OXl8ecOXMqXHZDcnJy+PnPf84ll1zC7NmzWbduHaNHj+bbb7/ljDPOYPjw4QwZMoQ1a9awaNEiJkyYsEXb2ZAZM2bw6KOPsm7dOp544gmaNGnCTjvtlLL1S5LiYyMqSdqo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JvFnP/sZxxxzDAsXLuTBBx+kd+/elXoUyYoVK+jatSunnXYas2fPZu7cudx+++3UrFlzk+/r1KkTLVq04IEHHmDevHnlNXToUKZOnfqD61M35sYbb2TGjBmUlJRQXFzMk08+ucnl161bR48ePWjfvj0lJSUsXLiQRx99lO222w6Ao48+mokTJ7J8+XLuvfdeTjvtNL755psKc7z77rsbvDazon1z1llnUVpaytKlSznvvPM488wzAdhjjz0YPnw4K1asYPTo0Tz44IOMGjWqwhyjR4/miCOO4IgjjmD69Ol89dVXPPLII7z22msAfP7559x0000MHz6czz///EfXnvbr149PPvmEcePGsWjRIm6//XZyc3OZOXMmxx57LJdffjmLFi1i/PjxG2xEp0yZwuDBg5k+fTqLFy+mcePGPPfccwB89dVX/Pvf/67wM0iSMksO0QNFJUmSJElKC8+ISpIkSZLSykZUkiRJkpRWNqKSJEmSpLSyEZUkSZIkpZWNqCRJkiQprWxEJUmSJElpZSMqSZIkSUorG1FJkiR
"text/plain": [
"<Figure size 1152x648 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"from matplotlib.patches import Arc, Rectangle, ConnectionPatch\n",
"from matplotlib.offsetbox import OffsetImage\n",
"plt.style.use('dark_background')\n",
"fig=plt.figure() #set up the figures\n",
"fig.set_size_inches(16, 9)\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.axis('off')\n",
"y_cary_end = 0\n",
"y_loc = 0\n",
"for i in range(len(Player)):\n",
" # # y - y' = -2(y' +c) reflection \n",
" #mirror image of a point https://math.stackexchange.com/questions/1013230/how-to-find-coordinates-of-reflected-point\n",
" y_cary_end = -2*(Player.iloc[i]['carry_end_location'][1] - 40) + Player.iloc[i]['carry_end_location'][1] \n",
" y_loc = -2*(Player.iloc[i]['location'][1] - 40) + Player.iloc[i]['location'][1] \n",
" if Player.iloc[i]['carry_end_location'][0] >= 90 and Player.iloc[i]['dribble_distance'] >= length:\n",
" ax.annotate(\"\", xy = (Player.iloc[i]['carry_end_location'][0],y_cary_end), xycoords = 'data',\n",
" xytext = (Player.iloc[i]['location'][0],y_loc ), textcoords = 'data',\n",
" arrowprops=dict(arrowstyle=\"->\",connectionstyle=\"arc3\", color = \"blue\"),)\n",
" ax.set_title(player_name)\n",
" ax.text(40, -5, 'dribbles '+ season, fontsize=14)\n",
"fname = 'C:\\\\Users\\\\koushik.r\\\\Desktop\\\\Messi data\\\\'+ player_name + str(' dribbles ')+ str(ssn)+'.png'\n",
"plt.savefig(fname,orientation='landscape')\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.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}