.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples_gallery/plot_gear_shifts_on_track.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_gallery_plot_gear_shifts_on_track.py: Gear shifts on track ======================= Plot which gear is being used at which point of the track .. GENERATED FROM PYTHON SOURCE LINES 7-8 Import FastF1 and load the data .. GENERATED FROM PYTHON SOURCE LINES 8-23 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from matplotlib import colormaps from matplotlib.collections import LineCollection import fastf1 session = fastf1.get_session(2021, 'Austrian Grand Prix', 'Q') session.load() lap = session.laps.pick_fastest() tel = lap.get_telemetry() .. GENERATED FROM PYTHON SOURCE LINES 25-27 Prepare the data for plotting by converting it to the appropriate numpy data types .. GENERATED FROM PYTHON SOURCE LINES 27-35 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 37-39 Create a line collection. Set a segmented colormap and normalize the plot to full integer values of the colormap .. GENERATED FROM PYTHON SOURCE LINES 39-45 .. code-block:: Python cmap = colormaps['Paired'] lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap) lc_comp.set_array(gear) lc_comp.set_linewidth(4) .. GENERATED FROM PYTHON SOURCE LINES 47-48 Create the plot .. GENERATED FROM PYTHON SOURCE LINES 48-59 .. code-block:: Python 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}" ) .. GENERATED FROM PYTHON SOURCE LINES 61-63 Add a colorbar to the plot. Shift the colorbar ticks by +0.5 so that they are centered for each color segment. .. GENERATED FROM PYTHON SOURCE LINES 63-71 .. code-block:: Python 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() .. image-sg:: /examples_gallery/images/sphx_glr_plot_gear_shifts_on_track_001.png :alt: Fastest Lap Gear Shift Visualization VER - Austrian Grand Prix 2021 :srcset: /examples_gallery/images/sphx_glr_plot_gear_shifts_on_track_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.802 seconds) .. _sphx_glr_download_examples_gallery_plot_gear_shifts_on_track.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gear_shifts_on_track.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gear_shifts_on_track.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_