formula1 / f1.py
kbberendsen's picture
add laptimes
b3cc530
import fastf1 as ff1
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib import cm
import numpy as np
import pandas as pd
ff1.Cache.enable_cache('.\cache')
session = ff1.get_session(2023, 'Austria', 'R')
session.load()
lap = session.laps.pick_fastest()
tel = lap.get_telemetry()
#converting data to numpy data tables
x = np.array(tel['X'].values)
y = np.array(tel['Y'].values)
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
gear = tel['nGear'].to_numpy().astype(float)
cmap = cm.get_cmap('Paired')
lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap)
lc_comp.set_array(gear)
lc_comp.set_linewidth(4)
plt.gca().add_collection(lc_comp)
plt.axis('equal')
plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)
title = plt.suptitle(
f"Fastest Lap Gear Shift Visualization\n"
f"{lap['Driver']} - {session.event['EventName']} {session.event.year}"
)
cbar = plt.colorbar(mappable=lc_comp, label="Gear", boundaries=np.arange(1, 10))
cbar.set_ticks(np.arange(1.5, 9.5))
cbar.set_ticklabels(np.arange(1, 9))
plt.show()
lap_time = lap['LapTime']
def format_timedelta(td):
delta_str= str(td)
# Split the time delta string to extract hours, minutes, and seconds
time_parts = delta_str.split(" ")[-1].split(":")
hours, minutes, seconds = map(float, time_parts)
# Convert the extracted values to the desired format
formatted_time = "{:02d}:{:06.3f}".format(int(hours * 60 + minutes), seconds)
return f"The lap time is: {formatted_time}"
print(format_timedelta(lap_time))