Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strategy example #372

Merged
merged 1 commit into from
May 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions examples/plot_strategy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""
Tyre strategies during a race
=============================

Plot all drivers' tyre strategies during a race.
"""

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

###############################################################################
# Load the race session

session = fastf1.get_session(2022, "Hungary", 'R')
session.load()
laps = session.laps

Casper-Guo marked this conversation as resolved.
Show resolved Hide resolved
###############################################################################
# Get the list of driver numbers
drivers = session.drivers
print(drivers)

###############################################################################
# Convert the driver numbers to three letter abbreviations
drivers = [session.get_driver(driver)["Abbreviation"] for driver in drivers]
print(drivers)

###############################################################################
# We need to find the stint length and compound used
theOehrly marked this conversation as resolved.
Show resolved Hide resolved
# for every stint by every driver.
# We do this by first grouping the laps by the driver,
# the stint number, and the compound.
# And then counting the number of laps in each group.
stints = laps[["Driver", "Stint", "Compound", "LapNumber"]]
stints = stints.groupby(["Driver", "Stint", "Compound"])
stints = stints.count().reset_index()

###############################################################################
# The number in the LapNumber column now stands for the number of observations
# in that group aka the stint length.
stints = stints.rename(columns={"LapNumber": "StintLength"})
print(stints)

###############################################################################
# Now we can plot the strategies for each driver
fig, ax = plt.subplots(figsize=(5, 10))

for driver in drivers:
driver_stints = stints.loc[stints["Driver"] == driver]

previous_stint_end = 0
for idx, row in driver_stints.iterrows():
# each row contains the compound name and stint length
# we can use these information to draw horizontal bars
plt.barh(
y=driver,
width=row["StintLength"],
left=previous_stint_end,
color=fastf1.plotting.COMPOUND_COLORS[row["Compound"]],
edgecolor="black",
fill=True
)

previous_stint_end += row["StintLength"]

# sphinx_gallery_defer_figures

###############################################################################
# Make the plot more readable and intuitive
plt.title("2022 Hungarian Grand Prix Strategies")
plt.xlabel("Lap Number")
plt.grid(False)
# invert the y-axis so drivers that finish higher are closer to the top
ax.invert_yaxis()

# sphinx_gallery_defer_figures

###############################################################################
# Plot aesthetics
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tight_layout()
plt.show()