{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\nimport plotly.express as px\nfrom plotly.offline import init_notebook_mode\nimport plotly.graph_objects as go\ninit_notebook_mode()\nimport ipywidgets as widgets","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-23T01:58:09.610820Z","iopub.execute_input":"2021-11-23T01:58:09.611096Z","iopub.status.idle":"2021-11-23T01:58:11.093791Z","shell.execute_reply.started":"2021-11-23T01:58:09.611065Z","shell.execute_reply":"2021-11-23T01:58:11.093174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking_20_df = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/tracking2020.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-23T01:58:43.105370Z","iopub.execute_input":"2021-11-23T01:58:43.105827Z","iopub.status.idle":"2021-11-23T01:59:03.473387Z","shell.execute_reply.started":"2021-11-23T01:58:43.105793Z","shell.execute_reply":"2021-11-23T01:59:03.472605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_play_in_game(gameId, speed=50):\n    temp_tracking_df = tracking_20_df[tracking_20_df['gameId'] == gameId]\n    for playId in temp_tracking_df['playId'].dropna().unique():\n        temp_tracking_query = (tracking_20_df['gameId'] == gameId) & (tracking_20_df['playId'] == playId)\n        temp_tracking_df = (\n            tracking_20_df[temp_tracking_query][['x', 'y', 'time', 'nflId', 'team', 'displayName']]\n            .fillna(0.)\n            .sort_values(['team', 'time'])\n        )\n        fig = px.scatter(\n            temp_tracking_df,\n            x='x',\n            y='y',\n            animation_frame='time',\n            color='team',\n            animation_group=\"nflId\",\n            hover_name=\"displayName\"\n        )\n        fig.update_traces(marker=dict(size=12,\n                                      line=dict(width=2,\n                                                color='DarkSlateGrey')),\n                          selector=dict(mode='markers'))\n\n        ## Drawing the Ground\n        for x in range(0, 130, 10):\n            fig.add_trace(go.Scatter(x=[x, x], y=[0, 53.3], mode='lines', showlegend=False, line=dict(color=\"#333333\")))\n        fig.add_trace(go.Scatter(x=[0, 120], y=[53.3, 53.3], mode='lines', showlegend=False, line=dict(color=\"#333333\")))\n        fig.add_trace(go.Scatter(x=[0, 120], y=[0, 0], mode='lines', showlegend=False, line=dict(color=\"#333333\")))\n        fig.update_layout(\n            autosize=False,\n            width=1100,\n            height=600,\n            title=f'Animation Every Players in the play {playId} of Game {gameId}',\n        )\n        fig.layout.updatemenus[0].buttons[0].args[1][\"frame\"][\"duration\"] = speed\n        fig.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:00:19.534228Z","iopub.execute_input":"2021-11-23T02:00:19.534488Z","iopub.status.idle":"2021-11-23T02:00:19.545153Z","shell.execute_reply.started":"2021-11-23T02:00:19.534458Z","shell.execute_reply":"2021-11-23T02:00:19.544169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gameId = 2021010300\nplot_play_in_game(gameId)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:00:22.986066Z","iopub.execute_input":"2021-11-23T02:00:22.986331Z","iopub.status.idle":"2021-11-23T02:00:54.465992Z","shell.execute_reply.started":"2021-11-23T02:00:22.986304Z","shell.execute_reply":"2021-11-23T02:00:54.465105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#TODO: ipwidgets to select playId and gameId","metadata":{"execution":{"iopub.status.busy":"2021-11-22T10:31:45.904448Z","iopub.execute_input":"2021-11-22T10:31:45.904676Z","iopub.status.idle":"2021-11-22T10:31:45.908475Z","shell.execute_reply.started":"2021-11-22T10:31:45.904648Z","shell.execute_reply":"2021-11-22T10:31:45.90761Z"},"trusted":true},"execution_count":null,"outputs":[]}]}