{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Integral as the area under a curve\n\nAlthough this is a simple example, it demonstrates some important tweaks:\n\n* A simple line plot with custom color and line width.\n* A shaded region created using a Polygon patch.\n* A text label with mathtext rendering.\n* figtext calls to label the x- and y-axes.\n* Use of axis spines to hide the top and right spines.\n* Custom tick placement and labels.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.patches import Polygon\n\n\ndef func(x):\n return (x - 3) * (x - 5) * (x - 7) + 85\n\n\na, b = 2, 9 # integral limits\nx = np.linspace(0, 10)\ny = func(x)\n\nfig, ax = plt.subplots()\nax.plot(x, y, 'r', linewidth=2)\nax.set_ylim(bottom=0)\n\n# Make the shaded region\nix = np.linspace(a, b)\niy = func(ix)\nverts = [(a, 0), *zip(ix, iy), (b, 0)]\npoly = Polygon(verts, facecolor='0.9', edgecolor='0.5')\nax.add_patch(poly)\n\nax.text(0.5 * (a + b), 30, r\"$\\int_a^b f(x)\\mathrm{d}x$\",\n horizontalalignment='center', fontsize=20)\n\nfig.text(0.9, 0.05, '$x$')\nfig.text(0.1, 0.9, '$y$')\n\nax.spines[['top', 'right']].set_visible(False)\nax.set_xticks([a, b], labels=['$a$', '$b$'])\nax.set_yticks([])\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 }