-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_position_changes.py
58 lines (43 loc) · 1.82 KB
/
plot_position_changes.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
"""
Position changes during a race
==============================
Plot the position of each driver at the end of each lap.
source: https://docs.fastf1.dev/examples_gallery/plot_position_changes.html#sphx-glr-examples-gallery-plot-position-changes-py
"""
import fastf1
import fastf1.plotting
import matplotlib.pyplot as plt
cache_dir = ".cache"
# Enable Cache
fastf1.Cache.enable_cache(cache_dir)
fastf1.plotting.setup_mpl(misc_mpl_mods=False)
##############################################################################
# Load the session and create the plot
session = fastf1.get_session(2023, 6, 'R')
session.load(telemetry=False, weather=False)
fig, ax = plt.subplots(figsize=(8.0, 4.9))
# sphinx_gallery_defer_figures
##############################################################################
# For each driver, get their three letter abbreviation (e.g. 'HAM') by simply
# using the value of the first lap, get their color and then plot their
# position over the number of laps.
for drv in session.drivers:
drv_laps = session.laps.pick_driver(drv)
abb = drv_laps['Driver'].iloc[0]
color = fastf1.plotting.driver_color(abb)
ax.plot(drv_laps['LapNumber'], drv_laps['Position'],
label=abb, color=color)
# sphinx_gallery_defer_figures
##############################################################################
# Finalize the plot by setting y-limits that invert the y-axis so that position
# one is at the top, set custom tick positions and axis labels.
ax.set_ylim([20.5, 0.5])
ax.set_yticks([1, 5, 10, 15, 20])
ax.set_xlabel('Lap')
ax.set_ylabel('Position')
# sphinx_gallery_defer_figures
##############################################################################
# Because this plot is very crowed, add the legend outside the plot area.
ax.legend(bbox_to_anchor=(1.0, 1.02))
plt.tight_layout()
plt.show()