{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Imshow\n",
    "\n",
    "`imshow` is great for seeing how a 2D function will respond to parameters, or for tasks like thresholding an image. If you want to look at a slices of a precomputed array you should consider using [hyperslicer](./hyperslicer.ipynb), which was created for exactly that purpose."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib ipympl\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "from mpl_interactions import ipyplot as iplt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.linspace(0, np.pi, 200)\n",
    "y = np.linspace(0, 10, 200)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "\n",
    "\n",
    "def f(param1, param2):\n",
    "    return np.sin(X) * param2 + np.exp(np.cos(Y * param1)) + param2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "controls = iplt.imshow(f, param1=(-5, 5), param2=(-3, 12))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](../_static/images/imshow-basic.apng)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Providing an axis\n",
    "\n",
    "You can also embed the interactive plot into an existing figure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))\n",
    "\n",
    "ax1.plot(np.sin(np.linspace(0, np.pi)))\n",
    "controls2 = iplt.imshow(f, param1=(-5, 5), param2=(-3, 12), ax=ax2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](../_static/images/imshow-subplots.apng)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Preventing colormap autoscaling\n",
    "\n",
    "The if you do not specify vmin/vmax and your function does not return an RGB(A) image then the default behavior is to rescale the colormap for each parameter change. This can disabled using this `autoscale_cmap` argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "fig3, ax3 = plt.subplots()\n",
    "controls3 = iplt.imshow(f, param1=(-5, 5), param2=(-3, 12), autoscale_cmap=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](../_static/images/imshow-no-cmap-auto.apng)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### vmin and vmax: thresholding an image\n",
    "\n",
    "Here we use the `vmin_vmax` argument along with the syntax for a RangeSlider to easily threshold the image. For more on how to use RangeSliders see [their example page](range-sliders.ipynb). Additionally you do not need to use a function to provide the image, you can also provide an array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "img = plt.imread(\"https://matplotlib.org/3.3.1/_images/stinkbug.png\")\n",
    "\n",
    "\n",
    "fig4, ax4 = plt.subplots()\n",
    "controls4 = iplt.imshow(img, vmin_vmax=(\"r\", img.min(), img.max()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](../_static/images/imshow-threshold.apng)"
   ]
  }
 ],
 "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.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
