{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Colorbar\n\nUse `~.Figure.colorbar` by specifying the mappable object (here\nthe `.AxesImage` returned by `~.axes.Axes.imshow`)\nand the Axes to attach the colorbar to.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\n# setup some generic data\nN = 37\nx, y = np.mgrid[:N, :N]\nZ = (np.cos(x*0.2) + np.sin(y*0.3))\n\n# mask out the negative and positive values, respectively\nZpos = np.ma.masked_less(Z, 0)\nZneg = np.ma.masked_greater(Z, 0)\n\nfig, (ax1, ax2, ax3) = plt.subplots(figsize=(13, 3), ncols=3)\n\n# plot just the positive data and save the\n# color \"mappable\" object returned by ax1.imshow\npos = ax1.imshow(Zpos, cmap='Blues', interpolation='none')\n\n# add the colorbar using the figure's method,\n# telling which mappable we're talking about and\n# which Axes object it should be near\nfig.colorbar(pos, ax=ax1)\n\n# repeat everything above for the negative data\n# you can specify location, anchor and shrink the colorbar\nneg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none')\nfig.colorbar(neg, ax=ax2, location='right', anchor=(0, 0.3), shrink=0.7)\n\n# Plot both positive and negative values between +/- 1.2\npos_neg_clipped = ax3.imshow(Z, cmap='RdBu', vmin=-1.2, vmax=1.2,\n                             interpolation='none')\n# Add minorticks on the colorbar to make it easy to read the\n# values off the colorbar.\ncbar = fig.colorbar(pos_neg_clipped, ax=ax3, extend='both')\ncbar.minorticks_on()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. 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.colorbar.Colorbar.minorticks_on`\n   - `matplotlib.colorbar.Colorbar.minorticks_off`\n\n.. tags::\n\n   component: colorbar\n   styling: color\n   plot-type: imshow\n   level: beginner\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.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}