Skip to content

Commit

Permalink
DOC: add driver laptimes example (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper-Guo authored May 19, 2023
1 parent e9832ee commit 3ebeb28
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
61 changes: 61 additions & 0 deletions examples/plot_driver_laptimes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Driver Laptimes Scatterplot
==============================
Plot a driver's lap times in a race, with color coding for the compounds.
"""

import fastf1
import fastf1.plotting
import seaborn as sns
from matplotlib import pyplot as plt

# 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')
# sphinx_gallery_defer_figures

###############################################################################
# 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()
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ requests>=2.28.1
websockets>=10.3
seaborn
plotly
seaborn
kaleido
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ install_requires =
thefuzz
matplotlib>=3.4.2,<4.0.0
python-dateutil
timple>=0.1.2
timple>=0.1.6
requests>=2.28.0
websockets>=8.1

Expand Down

0 comments on commit 3ebeb28

Please sign in to comment.