{ "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": { "gif": "imshow-basic.apng" }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "controls = iplt.imshow(f, param1=(-5, 5), param2=(-3, 12))" ] }, { "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": { "gif": "imshow-subplots.apng" }, "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": [ "### 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": { "gif": "imshow-no-cmap-auto.apng" }, "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": [ "### vmin and vmax: thresholding an image\n", "\n", "You can also pass `vmin` and `vmax` as functions. 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": { "gif": "imshow-threshold.apng" }, "outputs": [], "source": [ "img = plt.imread(\"https://matplotlib.org/3.3.1/_images/stinkbug.png\")\n", "\n", "\n", "def vmin(min_, max_):\n", " return min(min_, max_)\n", "\n", "\n", "def vmax(min_, max_):\n", " return max(min_, max_)\n", "\n", "\n", "fig4, ax4 = plt.subplots()\n", "controls4 = iplt.imshow(img, vmin=vmin, vmax=vmax, min_=(0, 0.7), max_=(0.3, 1))" ] }, { "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.7.8" } }, "nbformat": 4, "nbformat_minor": 4 }