.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples_gallery/plot_strategy.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_strategy.py: Tyre strategies during a race ============================= Plot all drivers' tyre strategies during a race. .. GENERATED FROM PYTHON SOURCE LINES 7-14 .. code-block:: Python from matplotlib import pyplot as plt import fastf1 import fastf1.plotting .. GENERATED FROM PYTHON SOURCE LINES 15-16 Load the race session .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python session = fastf1.get_session(2022, "Hungary", 'R') session.load() laps = session.laps .. GENERATED FROM PYTHON SOURCE LINES 22-23 Get the list of driver numbers .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: Python drivers = session.drivers print(drivers) .. rst-class:: sphx-glr-script-out .. code-block:: none ['1', '44', '63', '55', '11', '16', '4', '14', '31', '5', '18', '10', '24', '47', '3', '20', '23', '6', '22', '77'] .. GENERATED FROM PYTHON SOURCE LINES 27-28 Convert the driver numbers to three letter abbreviations .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: Python drivers = [session.get_driver(driver)["Abbreviation"] for driver in drivers] print(drivers) .. rst-class:: sphx-glr-script-out .. code-block:: none ['VER', 'HAM', 'RUS', 'SAI', 'PER', 'LEC', 'NOR', 'ALO', 'OCO', 'VET', 'STR', 'GAS', 'ZHO', 'MSC', 'RIC', 'MAG', 'ALB', 'LAT', 'TSU', 'BOT'] .. GENERATED FROM PYTHON SOURCE LINES 32-37 We need to find the stint length and compound used for every stint by every driver. We do this by first grouping the laps by the driver, the stint number, and the compound. And then counting the number of laps in each group. .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: Python stints = laps[["Driver", "Stint", "Compound", "LapNumber"]] stints = stints.groupby(["Driver", "Stint", "Compound"]) stints = stints.count().reset_index() .. GENERATED FROM PYTHON SOURCE LINES 42-44 The number in the LapNumber column now stands for the number of observations in that group aka the stint length. .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: Python stints = stints.rename(columns={"LapNumber": "StintLength"}) print(stints) .. rst-class:: sphx-glr-script-out .. code-block:: none Driver Stint Compound StintLength 0 ALB 1.0 SOFT 2 1 ALB 2.0 MEDIUM 19 2 ALB 3.0 MEDIUM 19 3 ALB 4.0 SOFT 29 4 ALO 1.0 MEDIUM 21 .. ... ... ... ... 57 VET 2.0 MEDIUM 31 58 VET 3.0 MEDIUM 23 59 ZHO 1.0 MEDIUM 27 60 ZHO 2.0 HARD 24 61 ZHO 3.0 SOFT 18 [62 rows x 4 columns] .. GENERATED FROM PYTHON SOURCE LINES 48-49 Now we can plot the strategies for each driver .. GENERATED FROM PYTHON SOURCE LINES 49-70 .. code-block:: Python fig, ax = plt.subplots(figsize=(5, 10)) for driver in drivers: driver_stints = stints.loc[stints["Driver"] == driver] previous_stint_end = 0 for idx, row in driver_stints.iterrows(): # each row contains the compound name and stint length # we can use these information to draw horizontal bars plt.barh( y=driver, width=row["StintLength"], left=previous_stint_end, color=fastf1.plotting.COMPOUND_COLORS[row["Compound"]], edgecolor="black", fill=True ) previous_stint_end += row["StintLength"] .. GENERATED FROM PYTHON SOURCE LINES 72-73 Make the plot more readable and intuitive .. GENERATED FROM PYTHON SOURCE LINES 73-80 .. code-block:: Python plt.title("2022 Hungarian Grand Prix Strategies") plt.xlabel("Lap Number") plt.grid(False) # invert the y-axis so drivers that finish higher are closer to the top ax.invert_yaxis() .. GENERATED FROM PYTHON SOURCE LINES 82-83 Plot aesthetics .. GENERATED FROM PYTHON SOURCE LINES 83-89 .. code-block:: Python ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['left'].set_visible(False) plt.tight_layout() plt.show() .. image-sg:: /examples_gallery/images/sphx_glr_plot_strategy_001.png :alt: 2022 Hungarian Grand Prix Strategies :srcset: /examples_gallery/images/sphx_glr_plot_strategy_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.895 seconds) .. _sphx_glr_download_examples_gallery_plot_strategy.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_strategy.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_strategy.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_