{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Contour Demo\n\nIllustrate simple contour plotting, contours on an image with\na colorbar for the contours, and labelled contours.\n\nSee also the :doc:`contour image example\n</gallery/images_contours_and_fields/contour_image>`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\ndelta = 0.025\nx = np.arange(-3.0, 3.0, delta)\ny = np.arange(-2.0, 2.0, 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"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create a simple contour plot with labels using default colors.  The inline\nargument to clabel will control whether the labels are draw over the line\nsegments of the contour, removing the lines beneath the label.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots()\nCS = ax.contour(X, Y, Z)\nax.clabel(CS, fontsize=10)\nax.set_title('Simplest default with labels')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Contour labels can be placed manually by providing list of positions (in data\ncoordinate).  See :doc:`/gallery/event_handling/ginput_manual_clabel_sgskip`\nfor interactive placement.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots()\nCS = ax.contour(X, Y, Z)\nmanual_locations = [\n    (-1, -1.4), (-0.62, -0.7), (-2, 0.5), (1.7, 1.2), (2.0, 1.4), (2.4, 1.7)]\nax.clabel(CS, fontsize=10, manual=manual_locations)\nax.set_title('labels at selected locations')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can force all the contours to be the same color.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots()\nCS = ax.contour(X, Y, Z, 6, colors='k')  # Negative contours default to dashed.\nax.clabel(CS, fontsize=9)\nax.set_title('Single color - negative contours dashed')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can set negative contours to be solid instead of dashed:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.rcParams['contour.negative_linestyle'] = 'solid'\nfig, ax = plt.subplots()\nCS = ax.contour(X, Y, Z, 6, colors='k')  # Negative contours default to dashed.\nax.clabel(CS, fontsize=9)\nax.set_title('Single color - negative contours solid')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And you can manually specify the colors of the contour\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots()\nCS = ax.contour(X, Y, Z, 6,\n                linewidths=np.arange(.5, 4, .5),\n                colors=('r', 'green', 'blue', (1, 1, 0), '#afeeee', '0.5'),\n                )\nax.clabel(CS, fontsize=9)\nax.set_title('Crazy lines')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Or you can use a colormap to specify the colors; the default\ncolormap will be used for the contour lines\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots()\nim = ax.imshow(Z, interpolation='bilinear', origin='lower',\n               cmap=\"gray\", extent=(-3, 3, -2, 2))\nlevels = np.arange(-1.2, 1.6, 0.2)\nCS = ax.contour(Z, levels, origin='lower', cmap='flag', extend='both',\n                linewidths=2, extent=(-3, 3, -2, 2))\n\n# Thicken the zero contour.\nlws = np.resize(CS.get_linewidth(), len(levels))\nlws[6] = 4\nCS.set_linewidth(lws)\n\nax.clabel(CS, levels[1::2],  # label every second level\n          fmt='%1.1f', fontsize=14)\n\n# make a colorbar for the contour lines\nCB = fig.colorbar(CS, shrink=0.8)\n\nax.set_title('Lines with colorbar')\n\n# We can still add a colorbar for the image, too.\nCBI = fig.colorbar(im, orientation='horizontal', shrink=0.8)\n\n# This makes the original colorbar look a bit out of place,\n# so let's improve its position.\n\nl, b, w, h = ax.get_position().bounds\nll, bb, ww, hh = CB.ax.get_position().bounds\nCB.ax.set_position([ll, b + 0.1*h, ww, h*0.8])\n\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.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`\n   - `matplotlib.axes.Axes.clabel` / `matplotlib.pyplot.clabel`\n   - `matplotlib.axes.Axes.get_position`\n   - `matplotlib.axes.Axes.set_position`\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
}