.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gen_modules\examples_gallery\plot_annotate_speed_trace.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_gen_modules_examples_gallery_plot_annotate_speed_trace.py: Plot speed traces with corner annotations ============================================ Plot the speed over the course of a lap and add annotations to mark corners. .. GENERATED FROM PYTHON SOURCE LINES 6-22 .. code-block:: Python import matplotlib.pyplot as plt import fastf1.plotting # Enable Matplotlib patches for plotting timedelta values and load # FastF1's dark color scheme fastf1.plotting.setup_mpl(mpl_timedelta_support=True, misc_mpl_mods=False, color_scheme='fastf1') # load a session and its telemetry data session = fastf1.get_session(2021, 'Spanish Grand Prix', 'Q') session.load() .. GENERATED FROM PYTHON SOURCE LINES 23-25 First, we select the fastest lap and get the car telemetry data for this lap. .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: Python fastest_lap = session.laps.pick_fastest() car_data = fastest_lap.get_car_data().add_distance() .. GENERATED FROM PYTHON SOURCE LINES 30-32 Next, load the circuit info that includes the information about the location of the corners. .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: Python circuit_info = session.get_circuit_info() .. GENERATED FROM PYTHON SOURCE LINES 36-38 Finally, we create a plot and plot the speed trace as well as the corner markers. .. GENERATED FROM PYTHON SOURCE LINES 38-70 .. code-block:: Python team_color = fastf1.plotting.get_team_color(fastest_lap['Team'], session=session) fig, ax = plt.subplots() ax.plot(car_data['Distance'], car_data['Speed'], color=team_color, label=fastest_lap['Driver']) # Draw vertical dotted lines at each corner that range from slightly below the # minimum speed to slightly above the maximum speed. v_min = car_data['Speed'].min() v_max = car_data['Speed'].max() ax.vlines(x=circuit_info.corners['Distance'], ymin=v_min-20, ymax=v_max+20, linestyles='dotted', colors='grey') # Plot the corner number just below each vertical line. # For corners that are very close together, the text may overlap. A more # complicated approach would be necessary to reliably prevent this. for _, corner in circuit_info.corners.iterrows(): txt = f"{corner['Number']}{corner['Letter']}" ax.text(corner['Distance'], v_min-30, txt, va='center_baseline', ha='center', size='small') ax.set_xlabel('Distance in m') ax.set_ylabel('Speed in km/h') ax.legend() # Manually adjust the y-axis limits to include the corner numbers, because # Matplotlib does not automatically account for text that was manually added. ax.set_ylim([v_min - 40, v_max + 20]) plt.show() .. image-sg:: /gen_modules/examples_gallery/images/sphx_glr_plot_annotate_speed_trace_001.png :alt: plot annotate speed trace :srcset: /gen_modules/examples_gallery/images/sphx_glr_plot_annotate_speed_trace_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.625 seconds) .. _sphx_glr_download_gen_modules_examples_gallery_plot_annotate_speed_trace.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_annotate_speed_trace.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_annotate_speed_trace.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_annotate_speed_trace.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_