{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Image with masked values\n\nimshow with masked array input and out-of-range colors.\n\nThe second subplot illustrates the use of BoundaryNorm to\nget a filled contour effect.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.colors as colors\n\n# compute some interesting data\nx0, x1 = -5, 5\ny0, y1 = -3, 3\nx = np.linspace(x0, x1, 500)\ny = np.linspace(y0, y1, 500)\nX, Y = np.meshgrid(x, y)\nZ1 = np.exp(-X**2 - Y**2)\nZ2 = np.exp(-(X - 1)**2 - (Y - 1)**2)\nZ = (Z1 - Z2) * 2\n\n# Set up a colormap:\npalette = plt.colormaps[\"gray\"].with_extremes(over='r', under='g', bad='b')\n# Alternatively, we could use\n# palette.set_bad(alpha = 0.0)\n# to make the bad region transparent.  This is the default.\n# If you comment out all the palette.set* lines, you will see\n# all the defaults; under and over will be colored with the\n# first and last colors in the palette, respectively.\nZm = np.ma.masked_where(Z > 1.2, Z)\n\n# By setting vmin and vmax in the norm, we establish the\n# range to which the regular palette color scale is applied.\n# Anything above that range is colored based on palette.set_over, etc.\n\n# set up the Axes objects\nfig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(6, 5.4))\n\n# plot using 'continuous' colormap\nim = ax1.imshow(Zm, interpolation='bilinear',\n                cmap=palette,\n                norm=colors.Normalize(vmin=-1.0, vmax=1.0),\n                aspect='auto',\n                origin='lower',\n                extent=[x0, x1, y0, y1])\nax1.set_title('Green=low, Red=high, Blue=masked')\ncbar = fig.colorbar(im, extend='both', shrink=0.9, ax=ax1)\ncbar.set_label('uniform')\nax1.tick_params(axis='x', labelbottom=False)\n\n# Plot using a small number of colors, with unevenly spaced boundaries.\nim = ax2.imshow(Zm, interpolation='nearest',\n                cmap=palette,\n                norm=colors.BoundaryNorm([-1, -0.5, -0.2, 0, 0.2, 0.5, 1],\n                                         ncolors=palette.N),\n                aspect='auto',\n                origin='lower',\n                extent=[x0, x1, y0, y1])\nax2.set_title('With BoundaryNorm')\ncbar = fig.colorbar(im, extend='both', spacing='proportional',\n                    shrink=0.9, ax=ax2)\ncbar.set_label('proportional')\n\nfig.suptitle('imshow, with out-of-range and masked data')\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.colors.BoundaryNorm`\n   - `matplotlib.colorbar.Colorbar.set_label`\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
}