{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Draw regions that span an Axes\n\n`~.Axes.axhspan` and `~.Axes.axvspan` draw rectangles that span the Axes in either\nthe horizontal or vertical direction and are bounded in the other direction. They are\noften used to highlight data regions.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(7, 3))\n\nnp.random.seed(19680801)\ns = 2.9 * np.convolve(np.random.randn(500), np.ones(30) / 30, mode='valid')\nax1.plot(s)\nax1.axhspan(-1, 1, alpha=0.1)\nax1.set(ylim=(-1.5, 1.5), title=\"axhspan\")\n\n\nmu = 8\nsigma = 2\nx = np.linspace(0, 16, 401)\ny = np.exp(-((x-mu)**2)/(2*sigma**2))\nax2.axvspan(mu-2*sigma, mu-sigma, color='0.95')\nax2.axvspan(mu-sigma, mu+sigma, color='0.9')\nax2.axvspan(mu+sigma, mu+2*sigma, color='0.95')\nax2.axvline(mu, color='darkgrey', linestyle='--')\nax2.plot(x, y)\nax2.set(title=\"axvspan\")\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.axhspan` / `matplotlib.pyplot.axhspan`\n - `matplotlib.axes.Axes.axvspan` / `matplotlib.pyplot.axvspan`\n\n\n.. seealso::\n\n `~.Axes.axhline`, `~.Axes.axvline`, `~.Axes.axline` draw infinite lines.\n\n.. tags::\n\n styling: shape\n plot-type: line\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 }