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

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

.. _sphx_glr_gallery_text_labels_and_annotations_custom_legends.py:


========================
Composing Custom Legends
========================

Composing custom legends piece-by-piece.

.. note::

   For more information on creating and customizing legends, see the following
   pages:

   * :ref:`legend_guide`
   * :doc:`/gallery/text_labels_and_annotations/legend_demo`

Sometimes you don't want a legend that is explicitly tied to data that
you have plotted. For example, say you have plotted 10 lines, but don't
want a legend item to show up for each one. If you simply plot the lines
and call ``ax.legend()``, you will get the following:

.. GENERATED FROM PYTHON SOURCE LINES 21-39

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    import matplotlib as mpl
    from matplotlib import cycler

    # Fixing random state for reproducibility
    np.random.seed(19680801)

    N = 10
    data = (np.geomspace(1, 10, 100) + np.random.randn(N, 100)).T
    cmap = plt.cm.coolwarm
    mpl.rcParams['axes.prop_cycle'] = cycler(color=cmap(np.linspace(0, 1, N)))

    fig, ax = plt.subplots()
    lines = ax.plot(data)
    ax.legend()




.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_custom_legends_001.png
   :alt: custom legends
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_custom_legends_001.png, /gallery/text_labels_and_annotations/images/sphx_glr_custom_legends_001_2_00x.png 2.00x
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.




.. GENERATED FROM PYTHON SOURCE LINES 41-44

Note that no legend entries were created.
In this case, we can compose a legend using Matplotlib objects that aren't
explicitly tied to the data that was plotted. For example:

.. GENERATED FROM PYTHON SOURCE LINES 44-56

.. code-block:: Python


    from matplotlib.lines import Line2D

    custom_lines = [Line2D([0], [0], color=cmap(0.), lw=4),
                    Line2D([0], [0], color=cmap(.5), lw=4),
                    Line2D([0], [0], color=cmap(1.), lw=4)]

    fig, ax = plt.subplots()
    lines = ax.plot(data)
    ax.legend(custom_lines, ['Cold', 'Medium', 'Hot'])





.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_custom_legends_002.png
   :alt: custom legends
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_custom_legends_002.png, /gallery/text_labels_and_annotations/images/sphx_glr_custom_legends_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 57-59

There are many other Matplotlib objects that can be used in this way. In the
code below we've listed a few common ones.

.. GENERATED FROM PYTHON SOURCE LINES 59-74

.. code-block:: Python


    from matplotlib.lines import Line2D
    from matplotlib.patches import Patch

    legend_elements = [Line2D([0], [0], color='b', lw=4, label='Line'),
                       Line2D([0], [0], marker='o', color='w', label='Scatter',
                              markerfacecolor='g', markersize=15),
                       Patch(facecolor='orange', edgecolor='r',
                             label='Color Patch')]

    # Create the figure
    fig, ax = plt.subplots()
    ax.legend(handles=legend_elements, loc='center')

    plt.show()



.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_custom_legends_003.png
   :alt: custom legends
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_custom_legends_003.png, /gallery/text_labels_and_annotations/images/sphx_glr_custom_legends_003_2_00x.png 2.00x
   :class: sphx-glr-single-img






.. _sphx_glr_download_gallery_text_labels_and_annotations_custom_legends.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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