{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Multiple images with one colorbar\n\nUse a single colorbar for multiple images.\n\nCurrently, a colorbar can only be connected to one image. The connection\nguarantees that the data coloring is consistent with the colormap scale\n(i.e. the color of value *x* in the colormap is used for coloring a data\nvalue *x* in the image).\n\nIf we want one colorbar to be representative for multiple images, we have\nto explicitly ensure consistent data coloring by using the same\ndata-to-color pipeline for all the images. We ensure this by explicitly\ncreating a `matplotlib.colorizer.Colorizer` object that we pass to all\nthe image plotting methods.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.colorizer as mcolorizer\nimport matplotlib.colors as mcolors\n\nnp.random.seed(19680801)\n\ndatasets = [\n    (i+1)/10 * np.random.rand(10, 20)\n    for i in range(4)\n]\n\nfig, axs = plt.subplots(2, 2)\nfig.suptitle('Multiple images')\n\n# create a colorizer with a predefined norm to be shared across all images\nnorm = mcolors.Normalize(vmin=np.min(datasets), vmax=np.max(datasets))\ncolorizer = mcolorizer.Colorizer(norm=norm)\n\nimages = []\nfor ax, data in zip(axs.flat, datasets):\n    images.append(ax.imshow(data, colorizer=colorizer))\n\nfig.colorbar(images[0], ax=axs, orientation='horizontal', fraction=.1)\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The colors are now kept consistent across all images when changing the\nscaling, e.g. through zooming in the colorbar or via the \"edit axis,\ncurves and images parameters\" GUI of the Qt backend. Additionally,\nif the colormap of the colorizer is changed, (e.g. through the \"edit\naxis, curves and images parameters\" GUI of the Qt backend) this change\npropagates to the other plots and the colorbar.\n\n.. 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.imshow` / `matplotlib.pyplot.imshow`\n   - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`\n   - `matplotlib.colorizer.Colorizer`\n   - `matplotlib.colors.Normalize`\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.14.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}