{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Contour Label Demo\n\nIllustrate some of the more advanced things that one can do with\ncontour labels.\n\nSee also the :doc:`contour demo example\n</gallery/images_contours_and_fields/contour_demo>`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.ticker as ticker"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define our surface\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "delta = 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": [
        "Make contour labels with custom level formatters\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# This custom formatter removes trailing zeros, e.g. \"1.0\" becomes \"1\", and\n# then adds a percent sign.\ndef fmt(x):\n    s = f\"{x:.1f}\"\n    if s.endswith(\"0\"):\n        s = f\"{x:.0f}\"\n    return rf\"{s} \\%\" if plt.rcParams[\"text.usetex\"] else f\"{s} %\"\n\n\n# Basic contour plot\nfig, ax = plt.subplots()\nCS = ax.contour(X, Y, Z)\n\nax.clabel(CS, CS.levels, fmt=fmt, fontsize=10)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Label contours with arbitrary strings using a dictionary\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig1, ax1 = plt.subplots()\n\n# Basic contour plot\nCS1 = ax1.contour(X, Y, Z)\n\nfmt = {}\nstrs = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh']\nfor l, s in zip(CS1.levels, strs):\n    fmt[l] = s\n\n# Label every other level using strings\nax1.clabel(CS1, CS1.levels[::2], fmt=fmt, fontsize=10)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use a Formatter\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig2, ax2 = plt.subplots()\n\nCS2 = ax2.contour(X, Y, 100**Z, locator=plt.LogLocator())\nfmt = ticker.LogFormatterMathtext()\nfmt.create_dummy_axis()\nax2.clabel(CS2, CS2.levels, fmt=fmt)\nax2.set_title(\"$100^Z$\")\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.axes.Axes.clabel` / `matplotlib.pyplot.clabel`\n   - `matplotlib.ticker.LogFormatterMathtext`\n   - `matplotlib.ticker.TickHelper.create_dummy_axis`\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.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}