sportowe_wizualizacja/538 LaLigaPredictions/538 Laliga Predictions Graph.ipynb

203 lines
40 KiB
Plaintext
Raw Normal View History

2020-10-20 10:05:57 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import matplotlib as mpl\n",
"import pandas as pd\n",
"import numpy as np\n",
"import seaborn as sns"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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>team</th>\n",
" <th>Win League</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Alavés</td>\n",
" <td>[1, 1]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Athletic Bilbao</td>\n",
" <td>[1, 1]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Atlético Madrid</td>\n",
" <td>[12, 17]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Barcelona</td>\n",
" <td>[44, 43]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Celta Vigo</td>\n",
" <td>[1, 1]</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" team Win League\n",
"0 Alavés [1, 1]\n",
"1 Athletic Bilbao [1, 1]\n",
"2 Atlético Madrid [12, 17]\n",
"3 Barcelona [44, 43]\n",
"4 Celta Vigo [1, 1]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv('la liga.csv',dtype={'team':'str','Win League':'str'})\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from ast import literal_eval\n",
"df['Win League'] = df['Win League'].apply(literal_eval)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# get the latest prob value\n",
"array_length = len(list(df.iloc[0]['Win League'])) #len of all lists should be same\n",
"def get_latest_prob(row):\n",
" row['latest_prob'] = int(row['Win League'][array_length -1])\n",
" return row\n",
"\n",
"df = df.apply(get_latest_prob,axis=1)\n",
"df =df.sort_values(by=['latest_prob'],ascending = False)[:5].copy()\n",
"#display(df)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkIAAAJRCAYAAACp5dg1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAgAElEQVR4nOzdd2BT5eLG8W/SkbSU0gWUUjYIKgqlTAe4cTAUFGTKkOG4P/XqVa/jCl69LrxeN3CvCrIdqICIKCKgIBRoGSpTluw26W66kt8fSdOktFCgoYU8n39sTs7J+zZg+/Ce55wYEhI7OBARERHxQ8bqnoCIiIhIdVEQEhEREb+lICQiIiJ+S0FIRERE/JaCkIiIiPgtBSERERHxWwpC1WDqlClsXL+Bjes30LtX7+qeznmjovdtwnMT3NvHjR3rk7ETExPdYyxasNAnY4iIyLkXeKodGjRowNcLF510n4cefphVP61yP77j9ju45ppraN6sGREREQSbTGRnZbFnzx6+X7aMzz77jKLiIu9xYmMZPnw4nTt3IbZ+fYKCgsjKymLX7t0sXvw1Xy1YUKlvaMJzE+jT2/lLcv2G9YwdN65Sx52tjes3uL9+bsIEFi46v35Zjhs7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y9OSXpaempp7WGGWF1grFkHry99pwGn8UhjI7Oxzlv4dnq+w4pyvD1Y0CKKrg9OWGDRsYOnwYfXr15pJLL6FJ4yZERkbSMbEjHRM70q5dOx7722NnNQ8REX91yiB0cZs27Ny1i6I
"text/plain": [
"<Figure size 576x576 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"\"\"\" Most of the code is taken from \n",
"https://github.com/petermckeeverPerform/friends-of-tracking-viz-lecture/blob/master/notebooks/lecture-notebook.ipynb\"\"\"\n",
"title_font = \"DejaVu Sans\"\n",
"body_font = \"Open Sans\"\n",
"text_color = \"w\"\n",
"background = \"#313332\"\n",
"filler = \"grey\"\n",
"primary = \"red\"\n",
"\n",
"mpl.rcParams['xtick.color'] = text_color\n",
"mpl.rcParams['ytick.color'] = text_color\n",
"mpl.rcParams['xtick.labelsize'] = 10\n",
"mpl.rcParams['ytick.labelsize'] = 10\n",
"array_length = len(list(df.iloc[0]['Win League'])) #len of all lists should be same\n",
"matchday = np.asarray(list(range(1, array_length +1)))\n",
"\n",
"fig, ax = plt.subplots(figsize=(8,8))\n",
"fig.set_facecolor(background)\n",
"ax.patch.set_alpha(0)\n",
"spines = [\"top\",\"right\",\"bottom\",\"left\"]\n",
"for s in spines:\n",
" if s in [\"top\",\"right\"]:\n",
" ax.spines[s].set_visible(False)\n",
" else:\n",
" ax.spines[s].set_color(text_color)\n",
"\n",
"ax.set_xlabel(\"Matchday\", fontfamily=title_font, fontweight=\"regular\", fontsize=16, color=text_color)\n",
"ax.set_ylabel(\"Percentage\", fontfamily=title_font, fontweight=\"regular\", fontsize= 16, color=text_color)\n",
"\n",
"ax.tick_params(axis=\"both\",length=0)\n",
"for i in range(len(df)):\n",
" x = np.asarray(list(df.iloc[i]['Win League']))\n",
" plt.annotate(df.iloc[i]['team'],(matchday[-1],np.asarray(list(df.iloc[i]['Win League']))[array_length -1]+0.5),color=text_color)\n",
" plt.plot(matchday,x)\n",
"\n",
"fig.text(0,1,\"538 La Liga Predictions\",fontweight=\"bold\", fontsize=18,fontfamily=title_font, color=text_color)\n",
"plt.tight_layout()\n",
"plt.show() \n",
"ax.grid(ls=\"dotted\",lw=\"0.5\",color=\"lightgrey\", zorder=1)"
]
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}