{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Zooming and Panning" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib widget\n", "import matplotlib.pyplot as plt\n", "\n", "from mpl_interactions import ioff, panhandler, zoom_factory" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load a sample image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A sample image\n", "image = plt.imread(\"https://github.com/matplotlib/matplotlib/raw/v3.3.0/lib/matplotlib/mpl-data/sample_data/ada.png\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## enable scroll to zoom\n", "\n", "Originally based on https://gist.github.com/tacaswell/3144287.\n", "\n", "To use just pass the axis object to the zoom factory function. Here I also demonstrate using `ioff` as a context manager so we can control where the figure canvas shows up." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with ioff:\n", " fig, ax = plt.subplots()\n", "ax.imshow(image)\n", "disconnect_zoom = zoom_factory(ax)\n", "display(fig.canvas)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Scrolling and panning\n", "\n", "\n", "I like being able to pan by clicking with the mouse without having to click the toolbar. Below is an example demonstrating using the `panhandler` object allow scrolling using the middle mouse button though by changing the `button` attribute you can use any mouse button. You need to make sure to assign the panhanler to a variable otherwise it will be garbage collected and will not work." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "gif": "zoom-and-pan.gif" }, "outputs": [], "source": [ "pan_handler = panhandler(fig)\n", "display(fig.canvas)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }