
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/text_labels_and_annotations/label_subplots.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_text_labels_and_annotations_label_subplots.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_text_labels_and_annotations_label_subplots.py:


==================
Labelling subplots
==================

Labelling subplots is relatively straightforward, and varies, so Matplotlib
does not have a general method for doing this.

We showcase two methods to position text at a given physical offset (in
fontsize units or in points) away from a corner of the Axes: one using
`~.Axes.annotate`, and one using `.ScaledTranslation`.

For convenience, this example uses `.pyplot.subplot_mosaic` and subplot
labels as keys for the subplots.  However, the approach also works with
`.pyplot.subplots` or keys that are different from what you want to label the
subplot with.

.. GENERATED FROM PYTHON SOURCE LINES 18-23

.. code-block:: Python


    import matplotlib.pyplot as plt

    from matplotlib.transforms import ScaledTranslation








.. GENERATED FROM PYTHON SOURCE LINES 24-39

.. code-block:: Python

    fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']],
                                  layout='constrained')
    for label, ax in axs.items():
        # Use Axes.annotate to put the label
        # - at the top left corner (axes fraction (0, 1)),
        # - offset half-a-fontsize right and half-a-fontsize down
        #   (offset fontsize (+0.5, -0.5)),
        # i.e. just inside the axes.
        ax.annotate(
            label,
            xy=(0, 1), xycoords='axes fraction',
            xytext=(+0.5, -0.5), textcoords='offset fontsize',
            fontsize='medium', verticalalignment='top', fontfamily='serif',
            bbox=dict(facecolor='0.7', edgecolor='none', pad=3.0))




.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_label_subplots_001.png
   :alt: label subplots
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_label_subplots_001.png, /gallery/text_labels_and_annotations/images/sphx_glr_label_subplots_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 40-52

.. code-block:: Python

    fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']],
                                  layout='constrained')
    for label, ax in axs.items():
        # Use ScaledTranslation to put the label
        # - at the top left corner (axes fraction (0, 1)),
        # - offset 20 pixels left and 7 pixels up (offset points (-20, +7)),
        # i.e. just outside the axes.
        ax.text(
            0.0, 1.0, label, transform=(
                ax.transAxes + ScaledTranslation(-20/72, +7/72, fig.dpi_scale_trans)),
            fontsize='medium', va='bottom', fontfamily='serif')




.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_label_subplots_002.png
   :alt: label subplots
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_label_subplots_002.png, /gallery/text_labels_and_annotations/images/sphx_glr_label_subplots_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 53-55

If we want it aligned with the title, either incorporate in the title or
use the *loc* keyword argument:

.. GENERATED FROM PYTHON SOURCE LINES 55-64

.. code-block:: Python


    fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']],
                                  layout='constrained')
    for label, ax in axs.items():
        ax.set_title('Normal Title', fontstyle='italic')
        ax.set_title(label, fontfamily='serif', loc='left', fontsize='medium')

    plt.show()




.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_label_subplots_003.png
   :alt: a), Normal Title, c), Normal Title, b), Normal Title, d), Normal Title
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_label_subplots_003.png, /gallery/text_labels_and_annotations/images/sphx_glr_label_subplots_003_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 65-74

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.figure.Figure.subplot_mosaic` /
     `matplotlib.pyplot.subplot_mosaic`
   - `matplotlib.axes.Axes.set_title`
   - `matplotlib.axes.Axes.annotate`


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.315 seconds)


.. _sphx_glr_download_gallery_text_labels_and_annotations_label_subplots.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: label_subplots.ipynb <label_subplots.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: label_subplots.py <label_subplots.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: label_subplots.zip <label_subplots.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
