

.. _sphx_glr_gallery_api_legend.py:


===============================
Legend using pre-defined labels
===============================

Notice how the legend labels are defined with the plots!




.. image:: /gallery/api/images/sphx_glr_legend_001.png
    :align: center





.. code-block:: python



    import numpy as np
    import matplotlib.pyplot as plt

    # Make some fake data.
    a = b = np.arange(0, 3, .02)
    c = np.exp(a)
    d = c[::-1]

    # Create plots with pre-defined labels.
    fig, ax = plt.subplots()
    ax.plot(a, c, 'k--', label='Model length')
    ax.plot(a, d, 'k:', label='Data length')
    ax.plot(a, c + d, 'k', label='Total message length')

    legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large')

    # Put a nicer background color on the legend.
    legend.get_frame().set_facecolor('#00FFCC')

    plt.show()

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



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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

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

    `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
