{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Controlling view limits using margins and sticky_edges\n\nThe first figure in this example shows how to zoom in and out of a\nplot using `~.Axes.margins` instead of `~.Axes.set_xlim` and\n`~.Axes.set_ylim`. The second figure demonstrates the concept of\nedge \"stickiness\" introduced by certain methods and artists and how\nto effectively work around that.\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 f(t):\n return np.exp(-t) * np.cos(2*np.pi*t)\n\n\nt1 = np.arange(0.0, 3.0, 0.01)\n\nax1 = plt.subplot(212)\nax1.margins(0.05) # Default margin is 0.05, value 0 means fit\nax1.plot(t1, f(t1))\n\nax2 = plt.subplot(221)\nax2.margins(2, 2) # Values >0.0 zoom out\nax2.plot(t1, f(t1))\nax2.set_title('Zoomed out')\n\nax3 = plt.subplot(222)\nax3.margins(x=0, y=-0.25) # Values in (-0.5, 0.0) zooms in to center\nax3.plot(t1, f(t1))\nax3.set_title('Zoomed in')\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## On the \"stickiness\" of certain plotting methods\n\nSome plotting functions make the axis limits \"sticky\" or immune to the will\nof the `~.Axes.margins` methods. For instance, `~.Axes.imshow` and\n`~.Axes.pcolor` expect the user to want the limits to be tight around the\npixels shown in the plot. If this behavior is not desired, you need to set\n`~.Axes.use_sticky_edges` to `False`. Consider the following example:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "y, x = np.mgrid[:5, 1:6]\npoly_coords = [\n (0.25, 2.75), (3.25, 2.75),\n (2.25, 0.75), (0.25, 0.75)\n]\nfig, (ax1, ax2) = plt.subplots(ncols=2)\n\n# Here we set the stickiness of the Axes object...\n# ax1 we'll leave as the default, which uses sticky edges\n# and we'll turn off stickiness for ax2\nax2.use_sticky_edges = False\n\nfor ax, status in zip((ax1, ax2), ('Is', 'Is Not')):\n cells = ax.pcolor(x, y, x+y, cmap='inferno', shading='auto') # sticky\n ax.add_patch(\n Polygon(poly_coords, color='forestgreen', alpha=0.5)\n ) # not sticky\n ax.margins(x=0.1, y=0.05)\n ax.set_aspect('equal')\n ax.set_title(f'{status} Sticky')\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.margins` / `matplotlib.pyplot.margins`\n - `matplotlib.axes.Axes.use_sticky_edges`\n - `matplotlib.axes.Axes.pcolor` / `matplotlib.pyplot.pcolor`\n - `matplotlib.patches.Polygon`\n\n.. tags::\n\n component: axes\n plot-type: line\n plot-type: imshow\n plot-type: pcolor\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 }