{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Compose custom legends\n\nComposing custom legends piece-by-piece.\n\n

Note

For more information on creating and customizing legends, see the following\n pages:\n\n * `legend_guide`\n * :doc:`/gallery/text_labels_and_annotations/legend_demo`

\n\nSometimes you don't want a legend that is explicitly tied to data that\nyou have plotted. For example, say you have plotted 10 lines, but don't\nwant a legend item to show up for each one. If you simply plot the lines\nand call ``ax.legend()``, you will get the following:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib as mpl\nfrom matplotlib import cycler\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "N = 10\ndata = (np.geomspace(1, 10, 100) + np.random.randn(N, 100)).T\ncmap = plt.cm.coolwarm\nmpl.rcParams['axes.prop_cycle'] = cycler(color=cmap(np.linspace(0, 1, N)))\n\nfig, ax = plt.subplots()\nlines = ax.plot(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since the data does not have any labels, creating a legend requires\nus to define the icons and labels.\nIn this case, we can compose a legend using Matplotlib objects that aren't\nexplicitly tied to the data that was plotted. For example:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from matplotlib.lines import Line2D\n\ncustom_lines = [Line2D([0], [0], color=cmap(0.), lw=4),\n Line2D([0], [0], color=cmap(.5), lw=4),\n Line2D([0], [0], color=cmap(1.), lw=4)]\n\nfig, ax = plt.subplots()\nlines = ax.plot(data)\nax.legend(custom_lines, ['Cold', 'Medium', 'Hot'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are many other Matplotlib objects that can be used in this way. In the\ncode below we've listed a few common ones.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from matplotlib.lines import Line2D\nfrom matplotlib.patches import Patch\n\nlegend_elements = [Line2D([0], [0], color='b', lw=4, label='Line'),\n Line2D([0], [0], marker='o', color='w', label='Scatter',\n markerfacecolor='g', markersize=15),\n Patch(facecolor='orange', edgecolor='r',\n label='Color Patch')]\n\n# Create the figure\nfig, ax = plt.subplots()\nax.legend(handles=legend_elements, loc='center')\n\nplt.show()" ] } ], "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 }