.. 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 :ref:`Go to the end ` 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-14 .. code-block:: Python import matplotlib as mpl import numpy as np from matplotlib import pyplot as plt from matplotlib.collections import LineCollection import fastf1 as ff1 .. GENERATED FROM PYTHON SOURCE LINES 15-17 First, we define some variables that allow us to conveniently control what we want to plot. .. GENERATED FROM PYTHON SOURCE LINES 17-24 .. code-block:: Python year = 2021 wknd = 9 ses = 'R' driver = 'RIC' colormap = mpl.cm.plasma .. GENERATED FROM PYTHON SOURCE LINES 25-26 Next, we load the session and select the desired data. .. GENERATED FROM PYTHON SOURCE LINES 26-37 .. code-block:: Python 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 .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/Fast-F1/Fast-F1/fastf1/core.py:478: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. /home/runner/work/Fast-F1/Fast-F1/fastf1/core.py:478: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. /home/runner/work/Fast-F1/Fast-F1/fastf1/core.py:478: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. .. GENERATED FROM PYTHON SOURCE LINES 38-42 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 42-46 .. code-block:: Python points = np.array([x, y]).T.reshape(-1, 1, 2) segments = np.concatenate([points[:-1], points[1:]], axis=1) .. GENERATED FROM PYTHON SOURCE LINES 47-48 After this, we can actually plot the data. .. GENERATED FROM PYTHON SOURCE LINES 48-84 .. code-block:: Python # 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: 9 2021 - RIC - Speed :srcset: /examples_gallery/images/sphx_glr_plot_speed_on_track_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.307 seconds) .. _sphx_glr_download_examples_gallery_plot_speed_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_speed_on_track.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_speed_on_track.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_