.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples_gallery/plot_qualifying_results.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_examples_gallery_plot_qualifying_results.py: Qualifying results overview ============================== Plot the qualifying result with visualization the fastest times. .. GENERATED FROM PYTHON SOURCE LINES 6-25 .. code-block:: Python import matplotlib.pyplot as plt import pandas as pd from timple.timedelta import strftimedelta import fastf1 import fastf1.plotting from fastf1.core import Laps # we only want support for timedelta plotting in this example fastf1.plotting.setup_mpl(mpl_timedelta_support=True, color_scheme=None, misc_mpl_mods=False) session = fastf1.get_session(2021, 'Spanish Grand Prix', 'Q') session.load() .. GENERATED FROM PYTHON SOURCE LINES 26-27 First, we need to get an array of all drivers. .. GENERATED FROM PYTHON SOURCE LINES 27-32 .. code-block:: Python drivers = pd.unique(session.laps['Driver']) print(drivers) .. rst-class:: sphx-glr-script-out .. code-block:: none ['HAM' 'VER' 'BOT' 'LEC' 'OCO' 'SAI' 'RIC' 'PER' 'NOR' 'ALO' 'STR' 'GAS' 'VET' 'GIO' 'RUS' 'TSU' 'RAI' 'MSC' 'LAT' 'MAZ'] .. GENERATED FROM PYTHON SOURCE LINES 33-36 After that we'll get each drivers fastest lap, create a new laps object from these laps, sort them by lap time and have pandas reindex them to number them nicely by starting position. .. GENERATED FROM PYTHON SOURCE LINES 36-46 .. code-block:: Python list_fastest_laps = list() for drv in drivers: drvs_fastest_lap = session.laps.pick_driver(drv).pick_fastest() list_fastest_laps.append(drvs_fastest_lap) fastest_laps = Laps(list_fastest_laps) \ .sort_values(by='LapTime') \ .reset_index(drop=True) .. GENERATED FROM PYTHON SOURCE LINES 47-50 The plot is nicer to look at and more easily understandable if we just plot the time differences. Therefore we subtract the fastest lap time from all other lap times. .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: Python pole_lap = fastest_laps.pick_fastest() fastest_laps['LapTimeDelta'] = fastest_laps['LapTime'] - pole_lap['LapTime'] .. GENERATED FROM PYTHON SOURCE LINES 56-59 We can take a quick look at the laps we have to check if everything looks all right. For this, we'll just check the 'Driver', 'LapTime' and 'LapTimeDelta' columns. .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. code-block:: Python print(fastest_laps[['Driver', 'LapTime', 'LapTimeDelta']]) .. rst-class:: sphx-glr-script-out .. code-block:: none Driver LapTime LapTimeDelta 0 HAM 0 days 00:01:16.741000 0 days 00:00:00 1 VER 0 days 00:01:16.777000 0 days 00:00:00.036000 2 BOT 0 days 00:01:16.873000 0 days 00:00:00.132000 3 LEC 0 days 00:01:17.510000 0 days 00:00:00.769000 4 OCO 0 days 00:01:17.580000 0 days 00:00:00.839000 5 SAI 0 days 00:01:17.620000 0 days 00:00:00.879000 6 RIC 0 days 00:01:17.622000 0 days 00:00:00.881000 7 PER 0 days 00:01:17.669000 0 days 00:00:00.928000 8 NOR 0 days 00:01:17.696000 0 days 00:00:00.955000 9 ALO 0 days 00:01:17.966000 0 days 00:00:01.225000 10 STR 0 days 00:01:17.974000 0 days 00:00:01.233000 11 GAS 0 days 00:01:17.982000 0 days 00:00:01.241000 12 VET 0 days 00:01:18.079000 0 days 00:00:01.338000 13 GIO 0 days 00:01:18.356000 0 days 00:00:01.615000 14 RUS 0 days 00:01:18.445000 0 days 00:00:01.704000 15 TSU 0 days 00:01:18.556000 0 days 00:00:01.815000 16 RAI 0 days 00:01:18.917000 0 days 00:00:02.176000 17 MSC 0 days 00:01:19.117000 0 days 00:00:02.376000 18 LAT 0 days 00:01:19.219000 0 days 00:00:02.478000 19 MAZ 0 days 00:01:19.807000 0 days 00:00:03.066000 .. GENERATED FROM PYTHON SOURCE LINES 64-65 Finally, we'll create a list of team colors per lap to color our plot. .. GENERATED FROM PYTHON SOURCE LINES 65-71 .. code-block:: Python team_colors = list() for index, lap in fastest_laps.iterlaps(): color = fastf1.plotting.team_color(lap['Team']) team_colors.append(color) .. rst-class:: sphx-glr-script-out .. code-block:: pytb Traceback (most recent call last): File "/home/runner/work/Fast-F1/Fast-F1/examples/plot_qualifying_results.py", line 67, in color = fastf1.plotting.team_color(lap['Team']) File "/home/runner/work/Fast-F1/Fast-F1/fastf1/plotting.py", line 372, in team_color raise KeyError KeyError .. GENERATED FROM PYTHON SOURCE LINES 72-73 Now, we can plot all the data .. GENERATED FROM PYTHON SOURCE LINES 73-87 .. code-block:: Python fig, ax = plt.subplots() ax.barh(fastest_laps.index, fastest_laps['LapTimeDelta'], color=team_colors, edgecolor='grey') ax.set_yticks(fastest_laps.index) ax.set_yticklabels(fastest_laps['Driver']) # show fastest at the top ax.invert_yaxis() # draw vertical lines behind the bars ax.set_axisbelow(True) ax.xaxis.grid(True, which='major', linestyle='--', color='black', zorder=-1000) .. GENERATED FROM PYTHON SOURCE LINES 89-90 Finally, give the plot a meaningful title .. GENERATED FROM PYTHON SOURCE LINES 90-97 .. code-block:: Python lap_time_string = strftimedelta(pole_lap['LapTime'], '%m:%s.%ms') plt.suptitle(f"{session.event['EventName']} {session.event.year} Qualifying\n" f"Fastest Lap: {lap_time_string} ({pole_lap['Driver']})") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.721 seconds) .. _sphx_glr_download_examples_gallery_plot_qualifying_results.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_qualifying_results.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_qualifying_results.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_