{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Fill the area between two vertical lines\n\nUsing `~.Axes.fill_betweenx` to color along the horizontal direction between\ntwo curves.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\ny = np.arange(0.0, 2, 0.01)\nx1 = np.sin(2 * np.pi * y)\nx2 = 1.2 * np.sin(4 * np.pi * y)\n\nfig, [ax1, ax2, ax3] = plt.subplots(1, 3, sharey=True, figsize=(6, 6))\n\nax1.fill_betweenx(y, 0, x1)\nax1.set_title('between (x1, 0)')\n\nax2.fill_betweenx(y, x1, 1)\nax2.set_title('between (x1, 1)')\nax2.set_xlabel('x')\n\nax3.fill_betweenx(y, x1, x2)\nax3.set_title('between (x1, x2)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now fill between x1 and x2 where a logical condition is met. Note this is\ndifferent than calling::\n\n fill_between(y[where], x1[where], x2[where])\n\nbecause of edge effects over multiple contiguous regions.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, [ax, ax1] = plt.subplots(1, 2, sharey=True, figsize=(6, 6))\nax.plot(x1, y, x2, y, color='black')\nax.fill_betweenx(y, x1, x2, where=x2 >= x1, facecolor='green')\nax.fill_betweenx(y, x1, x2, where=x2 <= x1, facecolor='red')\nax.set_title('fill_betweenx where')\n\n# Test support for masked arrays.\nx2 = np.ma.masked_greater(x2, 1.0)\nax1.plot(x1, y, x2, y, color='black')\nax1.fill_betweenx(y, x1, x2, where=x2 >= x1, facecolor='green')\nax1.fill_betweenx(y, x1, x2, where=x2 <= x1, facecolor='red')\nax1.set_title('regions with x2 > 1 are masked')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example illustrates a problem; because of the data gridding, there are\nundesired unfilled triangles at the crossover points. A brute-force solution\nwould be to interpolate all arrays to a very fine grid before plotting.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n\n plot-type: fill_between\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 }