Qualifying results overview

Plot the qualifying result with visualization the fastest times.

import matplotlib.pyplot as plt
import pandas as pd
from timple.timedelta import strftimedelta
import fastf1
import fastf1.plotting
from fastf1.core import Laps


fastf1.Cache.enable_cache('../doc_cache')  # replace with your cache directory

# 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()

First, we need to get an array of all drivers.

drivers = pd.unique(session.laps['Driver'])
print(drivers)
['HAM' 'VER' 'BOT' 'LEC' 'OCO' 'SAI' 'RIC' 'PER' 'NOR' 'ALO' 'STR' 'GAS'
 'VET' 'GIO' 'RUS' 'TSU' 'RAI' 'MSC' 'LAT' 'MAZ']

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.

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)

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.

pole_lap = fastest_laps.pick_fastest()
fastest_laps['LapTimeDelta'] = fastest_laps['LapTime'] - pole_lap['LapTime']

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.

print(fastest_laps[['Driver', 'LapTime', 'LapTimeDelta']])
   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.701000 0 days 00:00:00.960000
8     STR 0 days 00:01:17.974000 0 days 00:00:01.233000
9     GAS 0 days 00:01:17.982000 0 days 00:00:01.241000
10    NOR 0 days 00:01:18.010000 0 days 00:00:01.269000
11    VET 0 days 00:01:18.079000 0 days 00:00:01.338000
12    ALO 0 days 00:01:18.147000 0 days 00:00:01.406000
13    GIO 0 days 00:01:18.356000 0 days 00:00:01.615000
14    TSU 0 days 00:01:18.556000 0 days 00:00:01.815000
15    RAI 0 days 00:01:18.917000 0 days 00:00:02.176000
16    MSC 0 days 00:01:19.117000 0 days 00:00:02.376000
17    RUS 0 days 00:01:19.154000 0 days 00:00:02.413000
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

Finally, we’ll create a list of team colors per lap to color our plot.

team_colors = list()
for index, lap in fastest_laps.iterlaps():
    color = fastf1.plotting.team_color(lap['Team'])
    team_colors.append(color)

Now, we can plot all the data

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)

Finally, give the plot a meaningful title

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()
Spanish Grand Prix 2021 Qualifying Fastest Lap: 01:16.741 (HAM)

Total running time of the script: ( 0 minutes 2.839 seconds)

Gallery generated by Sphinx-Gallery