.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gen_modules\examples_gallery\plot_driver_styling.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_driver_styling.py: Driver specific plot styling =============================== Create some plots and show the usage of ``fastf1.plotting.get_driver_style``. .. GENERATED FROM PYTHON SOURCE LINES 6-19 .. code-block:: Python from matplotlib import pyplot as plt import fastf1 from fastf1 import 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') .. GENERATED FROM PYTHON SOURCE LINES 20-21 Load the race session. .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python race = fastf1.get_session(2023, "Azerbaijan", 'R') race.load() .. GENERATED FROM PYTHON SOURCE LINES 26-32 Basic driver-specific plot styling ---------------------------------- Plot all the laps for Hamilton, Russel, Perez and Verstappen. Filter out slow laps as they distort the graph axis. Note: as LapTime is represented by timedelta, calling ``setup_mpl`` earlier is required. .. GENERATED FROM PYTHON SOURCE LINES 32-47 .. code-block:: Python fig, ax = plt.subplots(figsize=(8, 5)) for driver in ('HAM', 'PER', 'VER', 'RUS'): laps = race.laps.pick_driver(driver).pick_quicklaps().reset_index() style = plotting.get_driver_style(identifier=driver, style=['color', 'linestyle'], session=race) ax.plot(laps['LapTime'], **style, label=driver) # add axis labels and a legend ax.set_xlabel("Lap Number") ax.set_ylabel("Lap Time") ax.legend() .. image-sg:: /gen_modules/examples_gallery/images/sphx_glr_plot_driver_styling_001.png :alt: plot driver styling :srcset: /gen_modules/examples_gallery/images/sphx_glr_plot_driver_styling_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 48-55 Sorting the legend ------------------ That plot looks pretty good already, but the order of the labels in the legend is slightly chaotic. Instead of trying to order the labels manually, use :func:`fastf1.plotting.add_sorted_driver_legend`. Let's create the exact same plot again, but this time with a sorted legend which means, we only change the very last function call. .. GENERATED FROM PYTHON SOURCE LINES 55-70 .. code-block:: Python fig, ax = plt.subplots(figsize=(8, 5)) for driver in ('HAM', 'PER', 'VER', 'RUS'): laps = race.laps.pick_driver(driver).pick_quicklaps().reset_index() style = plotting.get_driver_style(identifier=driver, style=['color', 'linestyle'], session=race) ax.plot(laps['LapTime'], **style, label=driver) # add axis labels and a legend ax.set_xlabel("Lap Number") ax.set_ylabel("Lap Time") plotting.add_sorted_driver_legend(ax, race) .. image-sg:: /gen_modules/examples_gallery/images/sphx_glr_plot_driver_styling_002.png :alt: plot driver styling :srcset: /gen_modules/examples_gallery/images/sphx_glr_plot_driver_styling_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 71-84 Creating fully custom styles ---------------------------- If you want to fully customize the plot style, you can define your own styling variants. Note that the value ``'auto'`` is treated as a magic keyword when used in combination with a color. It will be replaced with the team color. We define two styles, one for the first driver and one for the second driver in any team. The plot that is generated here isn't intended to be very readable, but it shows how you can customize any plot styling parameter. .. GENERATED FROM PYTHON SOURCE LINES 84-109 .. code-block:: Python my_styles = [ # style for each first driver {'color': 'auto', 'linestyle': 'solid', 'linewidth': 5, 'alpha': 0.3}, # style for each second driver {'color': 'auto', 'linestyle': 'solid', 'linewidth': 1, 'alpha': 0.7} ] fig, ax = plt.subplots(figsize=(8, 5)) for driver in ('HAM', 'PER', 'VER', 'RUS'): laps = race.laps.pick_driver(driver).pick_quicklaps().reset_index() # here, we now use ``style=my_style`` to use the custom styling style = plotting.get_driver_style(identifier=driver, style=my_styles, session=race) ax.plot(laps['LapTime'], **style, label=driver) # add axis labels and a legend ax.set_xlabel("Lap Number") ax.set_ylabel("Lap Time") plotting.add_sorted_driver_legend(ax, race) plt.show() .. image-sg:: /gen_modules/examples_gallery/images/sphx_glr_plot_driver_styling_003.png :alt: plot driver styling :srcset: /gen_modules/examples_gallery/images/sphx_glr_plot_driver_styling_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.016 seconds) .. _sphx_glr_download_gen_modules_examples_gallery_plot_driver_styling.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_driver_styling.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_driver_styling.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_driver_styling.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_