{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Polar legend\n\nUsing a legend on a polar-axis plot.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfig = plt.figure()\nax = fig.add_subplot(projection=\"polar\", facecolor=\"lightgoldenrodyellow\")\n\nr = np.linspace(0, 3, 301)\ntheta = 2 * np.pi * r\nax.plot(theta, r, color=\"tab:orange\", lw=3, label=\"a line\")\nax.plot(0.5 * theta, r, color=\"tab:blue\", ls=\"--\", lw=3, label=\"another line\")\nax.tick_params(grid_color=\"palegoldenrod\")\n# For polar Axes, it may be useful to move the legend slightly away from the\n# Axes center, to avoid overlap between the legend and the Axes. The following\n# snippet places the legend's lower left corner just outside the polar Axes\n# at an angle of 67.5 degrees in polar coordinates.\nangle = np.deg2rad(67.5)\nax.legend(loc=\"lower left\",\n bbox_to_anchor=(.5 + np.cos(angle)/2, .5 + np.sin(angle)/2))\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.plot` / `matplotlib.pyplot.plot`\n - `matplotlib.axes.Axes.legend` / `matplotlib.pyplot.legend`\n - `matplotlib.projections.polar`\n - `matplotlib.projections.polar.PolarAxes`\n\n.. tags::\n\n component: legend\n plot-type: polar\n level: beginner\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 }