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

    Click :ref:`here <sphx_glr_download_gallery_recipes_transparent_legends.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_recipes_transparent_legends.py:


Transparent, fancy legends
==========================

Sometimes you know what your data looks like before you plot it, and
may know for instance that there won't be much data in the upper right
hand corner.  Then you can safely create a legend that doesn't overlay
your data:

  ax.legend(loc='upper right')

Other times you don't know where your data is, and the default loc='best'
will try and place the legend::

  ax.legend()

but still, your legend may overlap your data, and in these cases it's
nice to make the legend frame transparent.




.. image:: /gallery/recipes/images/sphx_glr_transparent_legends_001.png
    :class: sphx-glr-single-img





.. code-block:: python


    import matplotlib.pyplot as plt
    import numpy as np

    np.random.seed(1234)
    fig, ax = plt.subplots(1)
    ax.plot(np.random.randn(300), 'o-', label='normal distribution')
    ax.plot(np.random.rand(300), 's-', label='uniform distribution')
    ax.set_ylim(-3, 3)

    ax.legend(fancybox=True, framealpha=0.5)
    ax.set_title('fancy, transparent legends')

    plt.show()


.. _sphx_glr_download_gallery_recipes_transparent_legends.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
