{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Figure legend demo\n\nRather than plotting a legend on each axis, a legend for all the artists\non all the sub-axes of a figure can be plotted instead.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfig, axs = plt.subplots(1, 2, layout='constrained')\n\nx = np.arange(0.0, 4*np.pi, 0.2)\naxs[0].plot(x, np.sin(x), label='Line 1')\naxs[0].plot(x, np.exp(-x/2), marker='o', label='Line 2')\naxs[1].plot(x, np.sin(x), color='tab:green', label='Line 3')\naxs[1].plot(x, np.exp(-x/4), color='tab:red', marker='^', label='Line 4')\n\nfig.legend(loc='outside right upper')\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The outside positioning is discussed in detail here:\nhttps://matplotlib.org/stable/users/explain/axes/legend_guide.html#figure-legends\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }