{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Contouring the solution space of optimizations\n\nContour plotting is particularly handy when illustrating the solution\nspace of optimization problems.  Not only can `.axes.Axes.contour` be\nused to represent the topography of the objective function, it can be\nused to generate boundary curves of the constraint functions.  The\nconstraint lines can be drawn with\n`~matplotlib.patheffects.TickedStroke` to distinguish the valid and\ninvalid sides of the constraint boundaries.\n\n`.axes.Axes.contour` generates curves with larger values to the left\nof the contour.  The angle parameter is measured zero ahead with\nincreasing values to the left.  Consequently, when using\n`~matplotlib.patheffects.TickedStroke` to illustrate a constraint in\na typical optimization problem, the angle should be set between\nzero and 180 degrees.\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 patheffects\n\nfig, ax = plt.subplots(figsize=(6, 6))\n\nnx = 101\nny = 105\n\n# Set up survey vectors\nxvec = np.linspace(0.001, 4.0, nx)\nyvec = np.linspace(0.001, 4.0, ny)\n\n# Set up survey matrices.  Design disk loading and gear ratio.\nx1, x2 = np.meshgrid(xvec, yvec)\n\n# Evaluate some stuff to plot\nobj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2\ng1 = -(3*x1 + x2 - 5.5)\ng2 = -(x1 + 2*x2 - 4.5)\ng3 = 0.8 + x1**-3 - x2\n\ncntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16],\n                  colors='black')\nax.clabel(cntr, fmt=\"%2.1f\", use_clabeltext=True)\n\ncg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown')\ncg1.set(path_effects=[patheffects.withTickedStroke(angle=135)])\n\ncg2 = ax.contour(x1, x2, g2, [0], colors='orangered')\ncg2.set(path_effects=[patheffects.withTickedStroke(angle=60, length=2)])\n\ncg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')\ncg3.set(path_effects=[patheffects.withTickedStroke(spacing=7)])\n\nax.set_xlim(0, 4)\nax.set_ylim(0, 4)\n\nplt.show()"
      ]
    }
  ],
  "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
}