{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Contour image\n\nTest combinations of contouring, filled contouring, and image plotting.\nFor contour labelling, see also the :doc:`contour demo example\n</gallery/images_contours_and_fields/contour_demo>`.\n\nThe emphasis in this demo is on showing how to make contours register\ncorrectly on images, and on how to get both of them oriented as desired.\nIn particular, note the usage of the `\"origin\" and \"extent\"\n<imshow_extent>` keyword arguments to imshow and\ncontour.\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 cm\n\n# Default delta is large because that makes it fast, and it illustrates\n# the correct registration between image and contours.\ndelta = 0.5\n\nextent = (-3, 4, -4, 3)\n\nx = np.arange(-3.0, 4.001, delta)\ny = np.arange(-4.0, 3.001, delta)\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# Boost the upper limit to avoid truncation errors.\nlevels = np.arange(-2.0, 1.601, 0.4)\n\nnorm = cm.colors.Normalize(vmax=abs(Z).max(), vmin=-abs(Z).max())\ncmap = plt.colormaps[\"PRGn\"]\n\nfig, _axs = plt.subplots(nrows=2, ncols=2)\nfig.subplots_adjust(hspace=0.3)\naxs = _axs.flatten()\n\ncset1 = axs[0].contourf(X, Y, Z, levels, norm=norm,\n                        cmap=cmap.resampled(len(levels) - 1))\n# It is not necessary, but for the colormap, we need only the\n# number of levels minus 1.  To avoid discretization error, use\n# either this number or a large number such as the default (256).\n\n# If we want lines as well as filled regions, we need to call\n# contour separately; don't try to change the edgecolor or edgewidth\n# of the polygons in the collections returned by contourf.\n# Use levels output from previous call to guarantee they are the same.\n\ncset2 = axs[0].contour(X, Y, Z, cset1.levels, colors='k')\n\n# We don't really need dashed contour lines to indicate negative\n# regions, so let's turn them off.\ncset2.set_linestyle('solid')\n\n# It is easier here to make a separate call to contour than\n# to set up an array of colors and linewidths.\n# We are making a thick green line as a zero contour.\n# Specify the zero level as a tuple with only 0 in it.\n\ncset3 = axs[0].contour(X, Y, Z, (0,), colors='g', linewidths=2)\naxs[0].set_title('Filled contours')\nfig.colorbar(cset1, ax=axs[0])\n\n\naxs[1].imshow(Z, extent=extent, cmap=cmap, norm=norm)\naxs[1].contour(Z, levels, colors='k', origin='upper', extent=extent)\naxs[1].set_title(\"Image, origin 'upper'\")\n\naxs[2].imshow(Z, origin='lower', extent=extent, cmap=cmap, norm=norm)\naxs[2].contour(Z, levels, colors='k', origin='lower', extent=extent)\naxs[2].set_title(\"Image, origin 'lower'\")\n\n# We will use the interpolation \"nearest\" here to show the actual\n# image pixels.\n# Note that the contour lines don't extend to the edge of the box.\n# This is intentional. The Z values are defined at the center of each\n# image pixel (each color block on the following subplot), so the\n# domain that is contoured does not extend beyond these pixel centers.\nim = axs[3].imshow(Z, interpolation='nearest', extent=extent,\n                   cmap=cmap, norm=norm)\naxs[3].contour(Z, levels, colors='k', origin='image', extent=extent)\nylim = axs[3].get_ylim()\naxs[3].set_ylim(ylim[::-1])\naxs[3].set_title(\"Origin from rc, reversed y-axis\")\nfig.colorbar(im, ax=axs[3])\n\nfig.tight_layout()\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.contour` / `matplotlib.pyplot.contour`\n   - `matplotlib.axes.Axes.imshow` / `matplotlib.pyplot.imshow`\n   - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`\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.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}