From 3ebeb28cf71d00f71ccbb91fe7402ef4347edffe Mon Sep 17 00:00:00 2001 From: Casper Guo <89810860+Casper-Guo@users.noreply.github.com> Date: Fri, 19 May 2023 17:31:04 -0400 Subject: [PATCH] DOC: add driver laptimes example (#369) --- examples/plot_driver_laptimes.py | 61 ++++++++++++++++++++++++++++++++ requirements-dev.txt | 1 + setup.cfg | 2 +- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100755 examples/plot_driver_laptimes.py diff --git a/examples/plot_driver_laptimes.py b/examples/plot_driver_laptimes.py new file mode 100755 index 000000000..0305d2002 --- /dev/null +++ b/examples/plot_driver_laptimes.py @@ -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() diff --git a/requirements-dev.txt b/requirements-dev.txt index 1cb0c3577..a933d7d64 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -15,4 +15,5 @@ requests>=2.28.1 websockets>=10.3 seaborn plotly +seaborn kaleido \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index a3b5a1c9d..b936b7842 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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