{
 "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(\n",
    "    \"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 {func}`~matplotlib.pyplot.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 the {class}`.panhandler` object. It allows scrolling using the middle mouse button, but with the `button` argument, you can change that to 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": {
    "tags": [
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "pan_handler = panhandler(fig)\n",
    "display(fig.canvas)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](../_static/images/zoom-and-pan.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It's also possible to manually enable and disable the `panhandler`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pan_handler.disable()\n",
    "pan_handler.enable()"
   ]
  }
 ],
 "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
}
