{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Polar plot\n\nDemo of a line plot on a polar axis.\n\nThe second plot shows the same data, but with the radial axis starting at r=1\nand the angular axis starting at 0 degrees and ending at 225 degrees. Setting\nthe origin of the radial axis to 0 allows the radial ticks to be placed at the\nsame location as the first plot.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nr = np.arange(0, 2, 0.01)\ntheta = 2 * np.pi * r\n\nfig, axs = plt.subplots(2, 1, figsize=(5, 8), subplot_kw={'projection': 'polar'},\n layout='constrained')\nax = axs[0]\nax.plot(theta, r)\nax.set_rmax(2)\nax.set_rticks([0.5, 1, 1.5, 2]) # Fewer radial ticks\nax.set_rlabel_position(-22.5) # Move radial labels away from plotted line\nax.grid(True)\n\nax.set_title(\"A line plot on a polar axis\", va='bottom')\n\nax = axs[1]\nax.plot(theta, r)\nax.set_rmax(2)\nax.set_rmin(1) # Change the radial axis to only go from 1 to 2\nax.set_rorigin(0) # Set the origin of the radial axis to 0\nax.set_thetamin(0)\nax.set_thetamax(225)\nax.set_rticks([1, 1.5, 2]) # Fewer radial ticks\nax.set_rlabel_position(-22.5) # Move radial labels away from plotted line\n\nax.grid(True)\nax.set_title(\"Same plot, but with reduced axis limits\", va='bottom')\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.projections.polar`\n - `matplotlib.projections.polar.PolarAxes`\n - `matplotlib.projections.polar.PolarAxes.set_rticks`\n - `matplotlib.projections.polar.PolarAxes.set_rmin`\n - `matplotlib.projections.polar.PolarAxes.set_rorigin`\n - `matplotlib.projections.polar.PolarAxes.set_rmax`\n - `matplotlib.projections.polar.PolarAxes.set_rlabel_position`\n\n.. tags::\n\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.5" } }, "nbformat": 4, "nbformat_minor": 0 }