-
Notifications
You must be signed in to change notification settings - Fork 3
/
LT.py
139 lines (104 loc) · 5.6 KB
/
LT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#<editor-fold desc="Imports">
import fastf1 as ff1
import fastf1.plotting
import matplotlib.pyplot as plt
import pandas as pd
#</editor-fold>
#<editor-fold desc="Set Ups">
ff1.Cache.enable_cache('Cache/')
pd.options.mode.chained_assignment = None
fastf1.plotting.setup_mpl()
plotSize = [15,15]
plotRatios = [3, 2, 1, 1, 2, 1]
#</editor-fold>
def DriverLapTelemetry(driver, year, race, session, lap, verbose):
#Gets The Laps From The Inputted Race Info
F1Session = ff1.get_session(year, race, session)
laps = F1Session.load_laps(with_telemetry=True)
#Finds The Specified Drivers Laps
driverLaps = laps.pick_driver(driver)
#Gets The Driver Fact Data (Team, Number Etc)
driverInfo = F1Session.get_driver(driver)
#Drivers Telemetry Data
driverTel = pd.DataFrame()
# Loops Through Every Lap In The Race To Find The Inputted One
for index, row in driverLaps.iterlaps():
if str(int(row['LapNumber'])) == lap:
driverTel = row.get_telemetry().add_distance()
#Increase The Plot Size
plt.rcParams['figure.figsize'] = plotSize
#Creates The Plot
fig, ax = plt.subplots(6, gridspec_kw = {'height_ratios': plotRatios}, sharex = True)
#Sets The Plots Title
ax[0].title.set_text("Lap " + lap + " Telemetry For " + driver + "\n" + F1Session.weekend.name + " " + str(F1Session.weekend.year))
#Plots
ax[0].plot(driverTel['Distance'], driverTel['Speed'], color = fastf1.plotting.team_color(driverInfo.team), label = driver)
ax[0].set(ylabel='Speed')
ax[1].plot(driverTel['Distance'], driverTel['Throttle'], color = fastf1.plotting.team_color(driverInfo.team), label = driver)
ax[1].set(ylabel='Throttle')
ax[2].plot(driverTel['Distance'], driverTel['Brake'], color = fastf1.plotting.team_color(driverInfo.team), label = driver)
ax[2].set(ylabel='Brake')
ax[3].plot(driverTel['Distance'], driverTel['nGear'], color=fastf1.plotting.team_color(driverInfo.team), label=driver)
ax[3].set(ylabel='Gear')
ax[4].plot(driverTel['Distance'], driverTel['RPM'], color=fastf1.plotting.team_color(driverInfo.team), label=driver)
ax[4].set(ylabel='RPM')
ax[5].plot(driverTel['Distance'], driverTel['DRS'], color=fastf1.plotting.team_color(driverInfo.team), label=driver)
ax[5].set(ylabel='DRS')
ax[5].set(xlabel='Distance In Meters')
#Cleans Up The Tick Labels
for a in ax:
a.label_outer()
#Outputs The Plot
plt.show()
def TwoDriverLapTelemetry(driver1, driver2, year, race, session, lap, verbose):
# Gets The Laps From The Inputted Race Info
F1Session = ff1.get_session(year, race, session)
laps = F1Session.load_laps(with_telemetry=True)
# Finds The Specified Drivers Laps
driver1Laps = laps.pick_driver(driver1)
driver2Laps = laps.pick_driver(driver2)
# Gets The Driver Fact Data (Team, Number Etc)
driver1Info = F1Session.get_driver(driver1)
driver2Info = F1Session.get_driver(driver2)
# Drivers Telemetry Data
driver1Tel = pd.DataFrame()
driver2Tel = pd.DataFrame()
# Loops Through Every Lap In The Race To Find The Inputted One
for index1, row1 in driver1Laps.iterlaps():
if str(int(row1['LapNumber'])) == lap:
driver1Tel = row1.get_telemetry().add_distance()
for index2, row2 in driver2Laps.iterlaps():
if str(int(row2['LapNumber'])) == lap:
driver2Tel = row2.get_telemetry().add_distance()
# Increase The Plot Size
plt.rcParams['figure.figsize'] = plotSize
# Creates The Plot
fig, ax = plt.subplots(6, gridspec_kw={'height_ratios': plotRatios}, sharex=True)
# Sets The Plots Title
ax[0].title.set_text("Lap " + lap + " Telemetry For " + driver1 + " & " + driver2 + "\n" + F1Session.weekend.name + " " + str(F1Session.weekend.year))
# Plots
ax[0].plot(driver1Tel['Distance'], driver1Tel['Speed'], color=fastf1.plotting.team_color(driver1Info.team), label=driver1)
ax[0].plot(driver2Tel['Distance'], driver2Tel['Speed'], color=fastf1.plotting.team_color(driver2Info.team), label=driver2)
ax[0].set(ylabel='Speed')
ax[0].legend(loc="lower right")
ax[1].plot(driver1Tel['Distance'], driver1Tel['Throttle'], color=fastf1.plotting.team_color(driver1Info.team), label=driver1)
ax[1].plot(driver2Tel['Distance'], driver2Tel['Throttle'], color=fastf1.plotting.team_color(driver2Info.team), label=driver2)
ax[1].set(ylabel='Throttle')
ax[2].plot(driver1Tel['Distance'], driver1Tel['Brake'], color=fastf1.plotting.team_color(driver1Info.team), label=driver1)
ax[2].plot(driver2Tel['Distance'], driver2Tel['Brake'], color=fastf1.plotting.team_color(driver2Info.team), label=driver2)
ax[2].set(ylabel='Brake')
ax[3].plot(driver1Tel['Distance'], driver1Tel['nGear'], color=fastf1.plotting.team_color(driver1Info.team), label=driver1)
ax[3].plot(driver2Tel['Distance'], driver2Tel['nGear'], color=fastf1.plotting.team_color(driver1Info.team), label=driver2)
ax[3].set(ylabel='Gear')
ax[4].plot(driver1Tel['Distance'], driver1Tel['RPM'], color=fastf1.plotting.team_color(driver1Info.team), label=driver1)
ax[4].plot(driver2Tel['Distance'], driver2Tel['RPM'], color=fastf1.plotting.team_color(driver2Info.team), label=driver2)
ax[4].set(ylabel='RPM')
ax[5].plot(driver1Tel['Distance'], driver1Tel['DRS'], color=fastf1.plotting.team_color(driver1Info.team), label=driver1)
ax[5].plot(driver2Tel['Distance'], driver2Tel['DRS'], color=fastf1.plotting.team_color(driver2Info.team), label=driver2)
ax[5].set(ylabel='DRS')
ax[5].set(xlabel='Distance In Meters')
# Cleans Up The Tick Labels
for a in ax:
a.label_outer()
# Outputs The Plot
plt.show()