.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gen_modules\examples_gallery\plot_results_tracker.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gen_modules_examples_gallery_plot_results_tracker.py: Plot driver standings in a heatmap ====================================== Plot the points for each driven in each race of a given season in a heatmap, as https://public.tableau.com/app/profile/mateusz.karmalski/viz/F1ResultsTracker2022 .. GENERATED FROM PYTHON SOURCE LINES 7-15 .. code-block:: Python import pandas as pd import plotly.express as px from plotly.io import show from fastf1.ergast import Ergast .. GENERATED FROM PYTHON SOURCE LINES 16-17 First, we load the results for season 2022. .. GENERATED FROM PYTHON SOURCE LINES 17-47 .. code-block:: Python ergast = Ergast() races = ergast.get_race_schedule(2022) # Races in year 2022 results = [] # For each race in the season for rnd, race in races['raceName'].items(): # Get results. Note that we use the round no. + 1, because the round no. # starts from one (1) instead of zero (0) temp = ergast.get_race_results(season=2022, round=rnd + 1) temp = temp.content[0] # If there is a sprint, get the results as well sprint = ergast.get_sprint_results(season=2022, round=rnd + 1) if sprint.content and sprint.description['round'][0] == rnd + 1: temp = pd.merge(temp, sprint.content[0], on='driverCode', how='left') # Add sprint points and race points to get the total temp['points'] = temp['points_x'] + temp['points_y'] temp.drop(columns=['points_x', 'points_y'], inplace=True) # Add round no. and grand prix name temp['round'] = rnd + 1 temp['race'] = race.removesuffix(' Grand Prix') temp = temp[['round', 'race', 'driverCode', 'points']] # Keep useful cols. results.append(temp) # Append all races into a single dataframe results = pd.concat(results) races = results['race'].drop_duplicates() .. rst-class:: sphx-glr-script-out .. code-block:: none Request for URL https://ergast.com/api/f1/2022/20/results.json failed; using cached response Traceback (most recent call last): File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\urllib3\connectionpool.py", line 536, in _make_request response = conn.getresponse() ^^^^^^^^^^^^^^^^^^ File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\urllib3\connection.py", line 464, in getresponse httplib_response = super().getresponse() ^^^^^^^^^^^^^^^^^^^^^ File "C:\Dev\py3.12.1_64\Lib\http\client.py", line 1419, in getresponse response.begin() File "C:\Dev\py3.12.1_64\Lib\http\client.py", line 331, in begin version, status, reason = self._read_status() ^^^^^^^^^^^^^^^^^^^ File "C:\Dev\py3.12.1_64\Lib\http\client.py", line 292, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Dev\py3.12.1_64\Lib\socket.py", line 707, in readinto return self._sock.recv_into(b) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Dev\py3.12.1_64\Lib\ssl.py", line 1253, in recv_into return self.read(nbytes, buffer) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Dev\py3.12.1_64\Lib\ssl.py", line 1105, in read return self._sslobj.read(len, buffer) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TimeoutError: The read operation timed out The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\requests\adapters.py", line 667, in send resp = conn.urlopen( ^^^^^^^^^^^^^ File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\urllib3\connectionpool.py", line 843, in urlopen retries = retries.increment( ^^^^^^^^^^^^^^^^^^ File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\urllib3\util\retry.py", line 474, in increment raise reraise(type(error), error, _stacktrace) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\urllib3\util\util.py", line 39, in reraise raise value File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\urllib3\connectionpool.py", line 789, in urlopen response = self._make_request( ^^^^^^^^^^^^^^^^^^^ File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\urllib3\connectionpool.py", line 538, in _make_request self._raise_timeout(err=e, url=url, timeout_value=read_timeout) File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\urllib3\connectionpool.py", line 369, in _raise_timeout raise ReadTimeoutError( urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='ergast.com', port=443): Read timed out. (read timeout=5.0) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\requests_cache\session.py", line 286, in _resend response = self._send_and_cache(request, actions, cached_response, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\requests_cache\session.py", line 254, in _send_and_cache response = super().send(request, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Dateien\Code\Formula1\FastF1\FastF1\src\fastf1\req.py", line 132, in send return super().send(request, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\requests\sessions.py", line 703, in send r = adapter.send(request, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Dateien\Code\Formula1\FastF1\FastF1\venvs\venv312\Lib\site-packages\requests\adapters.py", line 713, in send raise ReadTimeout(e, request=request) requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='ergast.com', port=443): Read timed out. (read timeout=5.0) .. GENERATED FROM PYTHON SOURCE LINES 48-50 Then we “reshape” the results to a wide table, where each row represents a driver and each column refers to a race, and the cell value is the points. .. GENERATED FROM PYTHON SOURCE LINES 50-62 .. code-block:: Python results = results.pivot(index='driverCode', columns='round', values='points') # Here we have a 22-by-22 matrix (22 races and 22 drivers, incl. DEV and HUL) # Rank the drivers by their total points results['total_points'] = results.sum(axis=1) results = results.sort_values(by='total_points', ascending=False) results.drop(columns='total_points', inplace=True) # Use race name, instead of round no., as column names results.columns = races .. GENERATED FROM PYTHON SOURCE LINES 63-64 The final step is to plot a heatmap using plotly .. GENERATED FROM PYTHON SOURCE LINES 64-90 .. code-block:: Python fig = px.imshow( results, text_auto=True, aspect='auto', # Automatically adjust the aspect ratio color_continuous_scale=[[0, 'rgb(198, 219, 239)'], # Blue scale [0.25, 'rgb(107, 174, 214)'], [0.5, 'rgb(33, 113, 181)'], [0.75, 'rgb(8, 81, 156)'], [1, 'rgb(8, 48, 107)']], labels={'x': 'Race', 'y': 'Driver', 'color': 'Points'} # Change hover texts ) fig.update_xaxes(title_text='') # Remove axis titles fig.update_yaxes(title_text='') fig.update_yaxes(tickmode='linear') # Show all ticks, i.e. driver names fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='LightGrey', showline=False, tickson='boundaries') # Show horizontal grid only fig.update_xaxes(showgrid=False, showline=False) # And remove vertical grid fig.update_layout(plot_bgcolor='rgba(0,0,0,0)') # White background fig.update_layout(coloraxis_showscale=False) # Remove legend fig.update_layout(xaxis=dict(side='top')) # x-axis on top fig.update_layout(margin=dict(l=0, r=0, b=0, t=0)) # Remove border margins fig show(fig) .. raw:: html :file: images\sphx_glr_plot_results_tracker_001.html .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 51.663 seconds) .. _sphx_glr_download_gen_modules_examples_gallery_plot_results_tracker.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_results_tracker.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_results_tracker.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_results_tracker.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_