.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gen_modules/examples_gallery/plot_position_changes.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_position_changes.py: Position changes during a race ============================== Plot the position of each driver at the end of each lap. .. GENERATED FROM PYTHON SOURCE LINES 7-17 .. code-block:: Python import matplotlib.pyplot as plt import fastf1.plotting # Load FastF1's dark color scheme fastf1.plotting.setup_mpl(mpl_timedelta_support=False, color_scheme='fastf1') .. GENERATED FROM PYTHON SOURCE LINES 18-19 Load the session and create the plot .. GENERATED FROM PYTHON SOURCE LINES 19-24 .. code-block:: Python session = fastf1.get_session(2023, 1, 'R') session.load(telemetry=False, weather=False) fig, ax = plt.subplots(figsize=(8.0, 4.9)) .. GENERATED FROM PYTHON SOURCE LINES 26-29 For each driver, get their three letter abbreviation (e.g. 'HAM') by simply using the value of the first lap, get their color and then plot their position over the number of laps. .. GENERATED FROM PYTHON SOURCE LINES 29-40 .. code-block:: Python for drv in session.drivers: drv_laps = session.laps.pick_drivers(drv) abb = drv_laps['Driver'].iloc[0] style = fastf1.plotting.get_driver_style(identifier=abb, style=['color', 'linestyle'], session=session) ax.plot(drv_laps['LapNumber'], drv_laps['Position'], label=abb, **style) .. GENERATED FROM PYTHON SOURCE LINES 42-44 Finalize the plot by setting y-limits that invert the y-axis so that position one is at the top, set custom tick positions and axis labels. .. GENERATED FROM PYTHON SOURCE LINES 44-49 .. code-block:: Python ax.set_ylim([20.5, 0.5]) ax.set_yticks([1, 5, 10, 15, 20]) ax.set_xlabel('Lap') ax.set_ylabel('Position') .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0, 0.5, 'Position') .. GENERATED FROM PYTHON SOURCE LINES 51-52 Because this plot is very crowed, add the legend outside the plot area. .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. code-block:: Python ax.legend(bbox_to_anchor=(1.0, 1.02)) plt.tight_layout() plt.show() .. image-sg:: /gen_modules/examples_gallery/images/sphx_glr_plot_position_changes_001.png :alt: plot position changes :srcset: /gen_modules/examples_gallery/images/sphx_glr_plot_position_changes_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.965 seconds) .. _sphx_glr_download_gen_modules_examples_gallery_plot_position_changes.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_position_changes.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_position_changes.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_position_changes.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_