sportowe_wizualizacja/538 LaLigaPredictions/538 Laliga Predictions Graph.ipynb

207 lines
45 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": 23,
2020-10-20 10:05:57 +02:00
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkIAAAJRCAYAAACp5dg1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAgAElEQVR4nOzdd3hUZd7G8e/0mfRCTYBQVVARQhFWBUEEdbEXFAQLCrZ91dXVLe7ad9XVdy1rwXdVEBVwFRug2BBUUEqCgPQWekkmPTPJtPePSQ4JSSABQhLm/lwXF5kzZ+acmUDmzvM8v98x9e6THkJEREQkApkb+wREREREGouCkIiIiEQsBSERERGJWApCIiIiErEUhERERCRiKQiJiIhIxFIQagSvT5pExtJlZCxdxsUjL27s02k2anvfHnn4EWP7xAkTGuTYffr0MY4x69PPGuQYx8Nll17G9Pem8f38Bcbr6dOnT2OflohIo7Eeboe2bdsy+7NZh9zn7nvu4fsfvjduX37Z5Zx77rl07tSJhIQE7A4HRYWFbNmyha+/+YYPPvgAf8Bf9Tht2jBu3Dj69z+TNq1bY7PZKCwsZOOmTcyZM5tPPv20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9pxD+XBBx40QlBZWRnTpk9j6bJlREdFceaZAygr85Gdnc3N48dz6aWXGKG48hTw4dYDDRo0qEoIeufdd/jp55/pk96HG8aNw2w2c+UVV/Lzz4v5+puvqz0+NTWVSa9P4td
2020-10-20 10:05:57 +02:00
"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",
"#this will hide the x-axis values\n",
"frame1 = plt.gca()\n",
"frame1.axes.get_xaxis().set_ticks([])\n",
"\n",
2020-10-20 10:05:57 +02:00
"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",
" ax.scatter(matchday,x,s=120,color='#841F27',edgecolors='#841F27', alpha=1, lw=0.25, zorder=4)\n",
"fig.text(0,1,\"538 La Liga Predictions for 2020/2021\",fontweight=\"bold\", fontsize=18,fontfamily=title_font, color=text_color)\n",
2020-10-20 10:05:57 +02:00
"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
}