.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples_gallery/plot_speed_on_track.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_gallery_plot_speed_on_track.py: Speed visualization on track map ====================================== (Example provided by @JSEHV on Github) .. GENERATED FROM PYTHON SOURCE LINES 6-17 .. code-block:: default import fastf1 as ff1 import numpy as np import matplotlib as mpl from matplotlib import pyplot as plt from matplotlib.collections import LineCollection ff1.Cache.enable_cache('../doc_cache') # replace with your cache directory .. GENERATED FROM PYTHON SOURCE LINES 18-20 First, we define some variables that allow us to conveniently control what we want to plot. .. GENERATED FROM PYTHON SOURCE LINES 20-27 .. code-block:: default year = 2021 wknd = 9 ses = 'R' driver = 'RIC' colormap = mpl.cm.plasma .. GENERATED FROM PYTHON SOURCE LINES 28-29 Next, we load the session and select the desired data. .. GENERATED FROM PYTHON SOURCE LINES 29-40 .. code-block:: default session = ff1.get_session(year, wknd, ses) weekend = session.event session.load() lap = session.laps.pick_driver(driver).pick_fastest() # Get telemetry data x = lap.telemetry['X'] # values for x-axis y = lap.telemetry['Y'] # values for y-axis color = lap.telemetry['Speed'] # value to base color gradient on .. GENERATED FROM PYTHON SOURCE LINES 41-45 Now, we create a set of line segments so that we can color them individually. This creates the points as a N x 1 x 2 array so that we can stack points together easily to get the segments. The segments array for line collection needs to be (numlines) x (points per line) x 2 (for x and y) .. GENERATED FROM PYTHON SOURCE LINES 45-49 .. code-block:: default points = np.array([x, y]).T.reshape(-1, 1, 2) segments = np.concatenate([points[:-1], points[1:]], axis=1) .. GENERATED FROM PYTHON SOURCE LINES 50-51 After this, we can actually plot the data. .. GENERATED FROM PYTHON SOURCE LINES 51-84 .. code-block:: default # We create a plot with title and adjust some setting to make it look good. fig, ax = plt.subplots(sharex=True, sharey=True, figsize=(12, 6.75)) fig.suptitle(f'{weekend.name} {year} - {driver} - Speed', size=24, y=0.97) # Adjust margins and turn of axis plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.12) ax.axis('off') # After this, we plot the data itself. # Create background track line ax.plot(lap.telemetry['X'], lap.telemetry['Y'], color='black', linestyle='-', linewidth=16, zorder=0) # Create a continuous norm to map from data points to colors norm = plt.Normalize(color.min(), color.max()) lc = LineCollection(segments, cmap=colormap, norm=norm, linestyle='-', linewidth=5) # Set the values used for colormapping lc.set_array(color) # Merge all line segments together line = ax.add_collection(lc) # Finally, we create a color bar as a legend. cbaxes = fig.add_axes([0.25, 0.05, 0.5, 0.05]) normlegend = mpl.colors.Normalize(vmin=color.min(), vmax=color.max()) legend = mpl.colorbar.ColorbarBase(cbaxes, norm=normlegend, cmap=colormap, orientation="horizontal") # Show the plot plt.show() .. image-sg:: /examples_gallery/images/sphx_glr_plot_speed_on_track_001.png :alt: Austrian Grand Prix 2021 - RIC - Speed :srcset: /examples_gallery/images/sphx_glr_plot_speed_on_track_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/runner/work/Fast-F1/Fast-F1/fastf1/events.py:650: FutureWarning: The `Weekend.name` property is deprecated and will beremoved in a future version. Use `Event['EventName']` or `Event.EventName` instead. warnings.warn( .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 6.707 seconds) .. _sphx_glr_download_examples_gallery_plot_speed_on_track.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_speed_on_track.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_speed_on_track.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_