.. 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 ` 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 ================== Instead of 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-32 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np fig, axs = plt.subplots(1, 2) x = np.arange(0.0, 2.0, 0.02) y1 = np.sin(2 * np.pi * x) y2 = np.exp(-x) l1, = axs[0].plot(x, y1) l2, = axs[0].plot(x, y2, marker='o') y3 = np.sin(4 * np.pi * x) y4 = np.exp(-2 * x) l3, = axs[1].plot(x, y3, color='tab:green') l4, = axs[1].plot(x, y4, color='tab:red', marker='^') fig.legend((l1, l2), ('Line 1', 'Line 2'), loc='upper left') fig.legend((l3, l4), ('Line 3', 'Line 4'), loc='upper right') plt.tight_layout() 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 33-36 Sometimes we do not want the legend to overlap the axes. If you use *constrained layout* you can specify "outside right upper", and *constrained layout* will make room for the legend. .. GENERATED FROM PYTHON SOURCE LINES 36-54 .. code-block:: Python fig, axs = plt.subplots(1, 2, layout='constrained') x = np.arange(0.0, 2.0, 0.02) y1 = np.sin(2 * np.pi * x) y2 = np.exp(-x) l1, = axs[0].plot(x, y1) l2, = axs[0].plot(x, y2, marker='o') y3 = np.sin(4 * np.pi * x) y4 = np.exp(-2 * x) l3, = axs[1].plot(x, y3, color='tab:green') l4, = axs[1].plot(x, y4, color='tab:red', marker='^') fig.legend((l1, l2), ('Line 1', 'Line 2'), loc='upper left') fig.legend((l3, l4), ('Line 3', 'Line 4'), loc='outside right upper') plt.show() .. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_figlegend_demo_002.png :alt: figlegend demo :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_figlegend_demo_002.png, /gallery/text_labels_and_annotations/images/sphx_glr_figlegend_demo_002_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.388 seconds) .. _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 ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: figlegend_demo.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_