Driver Laptimes Scatterplot#

Plot a driver’s lap times in a race, with color coding for the compounds.

import seaborn as sns
from matplotlib import pyplot as plt

import fastf1
import fastf1.plotting


# The misc_mpl_mods option enables minor grid lines which clutter the plot
fastf1.plotting.setup_mpl(misc_mpl_mods=False)

Load the race session.

race = fastf1.get_session(2023, "Azerbaijan", 'R')
race.load()

Get all the laps for a single driver. Filter out slow laps as they distort the graph axis.

driver_laps = race.laps.pick_driver("ALO").pick_quicklaps().reset_index()

Make the scattterplot using lap number as x-axis and lap time as y-axis. Marker colors correspond to the compounds used. Note: as LapTime is represented by timedelta, calling setup_mpl earlier is required.

fig, ax = plt.subplots(figsize=(8, 8))

sns.scatterplot(data=driver_laps,
                x="LapNumber",
                y="LapTime",
                ax=ax,
                hue="Compound",
                palette=fastf1.plotting.COMPOUND_COLORS,
                s=80,
                linewidth=0,
                legend='auto')
<Axes: xlabel='LapNumber', ylabel='LapTime'>

Make the plot more aesthetic.

ax.set_xlabel("Lap Number")
ax.set_ylabel("Lap Time")

# The y-axis increases from bottom to top by default
# Since we are plotting time, it makes sense to invert the axis
ax.invert_yaxis()
plt.suptitle("Alonso Laptimes in the 2023 Azerbaijan Grand Prix")

# Turn on major grid lines
plt.grid(color='w', which='major', axis='both')
sns.despine(left=True, bottom=True)

plt.tight_layout()
plt.show()
Alonso Laptimes in the 2023 Azerbaijan Grand Prix

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

Gallery generated by Sphinx-Gallery