{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Axes Grid2\n\nGrid of images with shared xaxis and yaxis.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib import cbook\nfrom mpl_toolkits.axes_grid1 import ImageGrid\n\n\ndef add_inner_title(ax, title, loc, **kwargs):\n    from matplotlib.offsetbox import AnchoredText\n    from matplotlib.patheffects import withStroke\n    prop = dict(path_effects=[withStroke(foreground='w', linewidth=3)],\n                size=plt.rcParams['legend.fontsize'])\n    at = AnchoredText(title, loc=loc, prop=prop,\n                      pad=0., borderpad=0.5,\n                      frameon=False, **kwargs)\n    ax.add_artist(at)\n    return at\n\n\nfig = plt.figure(figsize=(6, 6))\n\n# Prepare images\nZ = cbook.get_sample_data(\"axes_grid/bivariate_normal.npy\")\nextent = (-3, 4, -4, 3)\nZS = [Z[i::3, :] for i in range(3)]\nextent = extent[0], extent[1]/3., extent[2], extent[3]\n\n# *** Demo 1: colorbar at each Axes ***\ngrid = ImageGrid(\n    # 211 = at the position of fig.add_subplot(211)\n    fig, 211, nrows_ncols=(1, 3), axes_pad=0.05, label_mode=\"1\", share_all=True,\n    cbar_location=\"top\", cbar_mode=\"each\", cbar_size=\"7%\", cbar_pad=\"1%\")\ngrid[0].set(xticks=[-2, 0], yticks=[-2, 0, 2])\n\nfor i, (ax, z) in enumerate(zip(grid, ZS)):\n    im = ax.imshow(z, origin=\"lower\", extent=extent)\n    cb = ax.cax.colorbar(im)\n    # Changing the colorbar ticks\n    if i in [1, 2]:\n        cb.set_ticks([-1, 0, 1])\n\nfor ax, im_title in zip(grid, [\"Image 1\", \"Image 2\", \"Image 3\"]):\n    add_inner_title(ax, im_title, loc='lower left')\n\n# *** Demo 2: shared colorbar ***\ngrid2 = ImageGrid(\n    fig, 212, nrows_ncols=(1, 3), axes_pad=0.05, label_mode=\"1\", share_all=True,\n    cbar_location=\"right\", cbar_mode=\"single\", cbar_size=\"10%\", cbar_pad=0.05)\ngrid2[0].set(xlabel=\"X\", ylabel=\"Y\", xticks=[-2, 0], yticks=[-2, 0, 2])\n\nclim = (np.min(ZS), np.max(ZS))\nfor ax, z in zip(grid2, ZS):\n    im = ax.imshow(z, clim=clim, origin=\"lower\", extent=extent)\n\n# With cbar_mode=\"single\", cax attribute of all Axes are identical.\nax.cax.colorbar(im)\n\nfor ax, im_title in zip(grid2, [\"(a)\", \"(b)\", \"(c)\"]):\n    add_inner_title(ax, im_title, loc='upper left')\n\nplt.show()"
      ]
    }
  ],
  "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.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}