
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/text_labels_and_annotations/figlegend_demo.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_figlegend_demo.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_text_labels_and_annotations_figlegend_demo.py:


==================
Figure legend demo
==================

Rather than plotting a legend on each axis, a legend for all the artists
on all the sub-axes of a figure can be plotted instead.

.. GENERATED FROM PYTHON SOURCE LINES 9-25

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    fig, axs = plt.subplots(1, 2, layout='constrained')

    x = np.arange(0.0, 4*np.pi, 0.2)
    axs[0].plot(x, np.sin(x), label='Line 1')
    axs[0].plot(x, np.exp(-x/2), marker='o', label='Line 2')
    axs[1].plot(x, np.sin(x), color='tab:green', label='Line 3')
    axs[1].plot(x, np.exp(-x/4), color='tab:red', marker='^', label='Line 4')

    fig.legend(loc='outside right upper')

    plt.show()




.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_figlegend_demo_001.png
   :alt: figlegend demo
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_figlegend_demo_001.png, /gallery/text_labels_and_annotations/images/sphx_glr_figlegend_demo_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 26-28

The outside positioning is discussed in detail here:
https://matplotlib.org/stable/users/explain/axes/legend_guide.html#figure-legends


.. _sphx_glr_download_gallery_text_labels_and_annotations_figlegend_demo.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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