.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples_gallery/plot_laptimes_distribution.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_laptimes_distribution.py: Driver Laptimes Distribution Visualization ============================================= Visualizae different drivers' laptime distributions. .. GENERATED FROM PYTHON SOURCE LINES 5-16 .. code-block:: Python import seaborn as sns from matplotlib import pyplot as plt import fastf1 import fastf1.plotting # enabling misc_mpl_mods will turn on minor grid lines that clutters the plot fastf1.plotting.setup_mpl(mpl_timedelta_support=False, misc_mpl_mods=False) .. GENERATED FROM PYTHON SOURCE LINES 17-18 Load the race session .. GENERATED FROM PYTHON SOURCE LINES 18-22 .. code-block:: Python race = fastf1.get_session(2023, "Azerbaijan", 'R') race.load() .. GENERATED FROM PYTHON SOURCE LINES 23-26 Get all the laps for the point finishers only. Filter out slow laps (yellow flag, VSC, pitstops etc.) as they distort the graph axis. .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: Python point_finishers = race.drivers[:10] print(point_finishers) driver_laps = race.laps.pick_drivers(point_finishers).pick_quicklaps() driver_laps = driver_laps.reset_index() .. rst-class:: sphx-glr-script-out .. code-block:: none ['11', '1', '16', '14', '55', '44', '18', '63', '4', '22'] .. GENERATED FROM PYTHON SOURCE LINES 32-34 To plot the drivers by finishing order, we need to get their three-letter abbreviations in the finishing order. .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python finishing_order = [race.get_driver(i)["Abbreviation"] for i in point_finishers] print(finishing_order) .. rst-class:: sphx-glr-script-out .. code-block:: none ['PER', 'VER', 'LEC', 'ALO', 'SAI', 'HAM', 'STR', 'RUS', 'NOR', 'TSU'] .. GENERATED FROM PYTHON SOURCE LINES 38-42 We need to modify the DRIVER_COLORS palette. Its keys are the driver's full names but we need the keys to be the drivers' three-letter abbreviations. We can do this with the DRIVER_TRANSLATE mapping. .. GENERATED FROM PYTHON SOURCE LINES 42-46 .. code-block:: Python driver_colors = {abv: fastf1.plotting.DRIVER_COLORS[driver] for abv, driver in fastf1.plotting.DRIVER_TRANSLATE.items()} print(driver_colors) .. rst-class:: sphx-glr-script-out .. code-block:: none {'LEC': '#dc0000', 'SAI': '#ff8181', 'SHW': '#9c0000', 'VER': '#fcd700', 'PER': '#ffec7b', 'DEN': '#907400', 'PIA': '#ff8700', 'NOR': '#eeb370', 'OWA': '#ee6d3a', 'GAS': '#fe86bc', 'OCO': '#ff117c', 'DOO': '#894667', 'BOT': '#00e701', 'ZHO': '#008d01', 'POU': '#004601', 'DEV': '#1e3d61', 'TSU': '#356cac', 'RIC': '#2b4562', 'LAW': '#2b4562', 'HAD': '#1e6176', 'IWA': '#1e6176', 'MAG': '#ffffff', 'HUL': '#cacaca', 'BEA': '#c40000', 'ALO': '#006f62', 'STR': '#00413b', 'DRU': '#2f9b90', 'HAM': '#00d2be', 'RUS': '#24ffff', 'VES': '#00a6ff', 'ALB': '#005aff', 'SAR': '#012564', 'OSU': '#1b3d97'} .. GENERATED FROM PYTHON SOURCE LINES 47-49 First create the violin plots to show the distributions. Then use the swarm plot to show the actual laptimes. .. GENERATED FROM PYTHON SOURCE LINES 49-78 .. code-block:: Python # create the figure fig, ax = plt.subplots(figsize=(10, 5)) # Seaborn doesn't have proper timedelta support # so we have to convert timedelta to float (in seconds) driver_laps["LapTime(s)"] = driver_laps["LapTime"].dt.total_seconds() sns.violinplot(data=driver_laps, x="Driver", y="LapTime(s)", hue="Driver", inner=None, density_norm="area", order=finishing_order, palette=driver_colors ) sns.swarmplot(data=driver_laps, x="Driver", y="LapTime(s)", order=finishing_order, hue="Compound", palette=fastf1.plotting.COMPOUND_COLORS, hue_order=["SOFT", "MEDIUM", "HARD"], linewidth=0, size=4, ) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 80-81 Make the plot more aesthetic .. GENERATED FROM PYTHON SOURCE LINES 81-88 .. code-block:: Python ax.set_xlabel("Driver") ax.set_ylabel("Lap Time (s)") plt.suptitle("2023 Azerbaijan Grand Prix Lap Time Distributions") sns.despine(left=True, bottom=True) plt.tight_layout() plt.show() .. image-sg:: /examples_gallery/images/sphx_glr_plot_laptimes_distribution_001.png :alt: 2023 Azerbaijan Grand Prix Lap Time Distributions :srcset: /examples_gallery/images/sphx_glr_plot_laptimes_distribution_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.659 seconds) .. _sphx_glr_download_examples_gallery_plot_laptimes_distribution.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_laptimes_distribution.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_laptimes_distribution.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_