Last active
September 10, 2025 17:27
-
-
Save yanndebray/c6c0e1406153b3e7c4a5aecaa28dbf8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| session = pyrun("import fastf1; session = fastf1.get_session(2023, 1, 'R'); session.load(telemetry=False, weather=False)","session") | |
| drv = session.drivers; | |
| %% Figure / axes (dark theme similar to FastF1) | |
| figure('Color',[0.08 0.09 0.10],'Position',[100 100 900 500]); | |
| ax = axes('Parent', gcf); | |
| set(ax,'Color',[0.08 0.09 0.10],'XColor',[0.85 0.88 0.90], ... | |
| 'YColor',[0.85 0.88 0.90],'Box','off','LineWidth',1); | |
| hold(ax,'on'); grid(ax,'on'); ax.GridColor=[0.25 0.28 0.30]; | |
| % Pre-extract all drivers' lap/pos data + colors | |
| drivers = struct; | |
| maxLap = 0; | |
| for k = 1:length(drv) | |
| drv_id = drv(k); | |
| drv_laps = session.laps.pick_drivers(drv_id); | |
| % Abbreviation (e.g., 'HAM') | |
| abblist = drv_laps.get('Driver').unique().tolist(); | |
| abb = string(abblist{1}); | |
| d = pyrun("d={'solid':'-','dashed':'--'}","d"); | |
| % Get FastF1 style (hex color + linestyle) | |
| sty = pyrun("import fastf1.plotting;style = fastf1.plotting.get_driver_style(identifier=abb,style=['color', 'linestyle'],session=session)","style",abb=abb); | |
| hex = char(sty{'color'}); % e.g. '#00D2BE' | |
| ls = char(d.get(sty{'linestyle'})); % e.g. '-', '--', ':' | |
| lapNums = pylist2double(drv_laps.get('LapNumber').values.tolist()); | |
| posVals = pylist2double(drv_laps.get('Position').values.tolist()); | |
| drivers(k).abb = abb; | |
| drivers(k).color = hex2rgb(hex); | |
| drivers(k).ls = ls; | |
| drivers(k).laps = lapNums; | |
| drivers(k).pos = posVals; | |
| drivers(k).h = plot(ax,nan,nan,'LineWidth',1.5,'Color',drivers(k).color,... | |
| 'LineStyle',ls,'DisplayName',abb); | |
| maxLap = max(maxLap, max(lapNums)); | |
| end | |
| %% Axes styling | |
| set(ax,'YDir','reverse'); | |
| ylim(ax,[0.5,20.5]); | |
| yticks(ax,[1 5 10 15 20]); | |
| xlabel(ax,'Lap'); ylabel(ax,'Position'); | |
| legend(ax,'Location','eastoutside'); | |
| %% --- Animate lap-by-lap --- | |
| fps = 10; % frames per second | |
| pause_t = 1/fps; | |
| for L = 1:maxLap | |
| for k = 1:numel(drivers) | |
| % Plot only up to current lap | |
| mask = drivers(k).laps <= L; | |
| drivers(k).h.XData = drivers(k).laps(mask); | |
| drivers(k).h.YData = drivers(k).pos(mask); | |
| end | |
| title(ax, sprintf("Race Animation - Lap %d",L), 'Color', 'white'); | |
| drawnow; | |
| pause(pause_t); % control animation speed | |
| end | |
| %% --- Helpers --- | |
| function rgb = hex2rgb(hex) | |
| % Convert '#RRGGBB' to RGB in [0,1] | |
| if hex(1) == '#', hex = hex(2:end); end | |
| r = hex2dec(hex(1:2)); | |
| g = hex2dec(hex(3:4)); | |
| b = hex2dec(hex(5:6)); | |
| rgb = [r g b] / 255; | |
| end | |
| function v = pylist2double(pylist) | |
| % Convert a Python list of numbers to a MATLAB row vector of doubles | |
| c = cell(pylist); | |
| n = numel(c); | |
| v = zeros(1, n); | |
| for i = 1:n | |
| v(i) = double(c{i}); | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment