
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/images_contours_and_fields/contours_in_optimization_demo.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_images_contours_and_fields_contours_in_optimization_demo.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_images_contours_and_fields_contours_in_optimization_demo.py:


==============================================
Contouring the solution space of optimizations
==============================================

Contour plotting is particularly handy when illustrating the solution
space of optimization problems.  Not only can `.axes.Axes.contour` be
used to represent the topography of the objective function, it can be
used to generate boundary curves of the constraint functions.  The
constraint lines can be drawn with
`~matplotlib.patheffects.TickedStroke` to distinguish the valid and
invalid sides of the constraint boundaries.

`.axes.Axes.contour` generates curves with larger values to the left
of the contour.  The angle parameter is measured zero ahead with
increasing values to the left.  Consequently, when using
`~matplotlib.patheffects.TickedStroke` to illustrate a constraint in
a typical optimization problem, the angle should be set between
zero and 180 degrees.

.. GENERATED FROM PYTHON SOURCE LINES 21-62



.. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_contours_in_optimization_demo_001.png
   :alt: contours in optimization demo
   :srcset: /gallery/images_contours_and_fields/images/sphx_glr_contours_in_optimization_demo_001.png, /gallery/images_contours_and_fields/images/sphx_glr_contours_in_optimization_demo_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib import patheffects

    fig, ax = plt.subplots(figsize=(6, 6))

    nx = 101
    ny = 105

    # Set up survey vectors
    xvec = np.linspace(0.001, 4.0, nx)
    yvec = np.linspace(0.001, 4.0, ny)

    # Set up survey matrices.  Design disk loading and gear ratio.
    x1, x2 = np.meshgrid(xvec, yvec)

    # Evaluate some stuff to plot
    obj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2
    g1 = -(3*x1 + x2 - 5.5)
    g2 = -(x1 + 2*x2 - 4.5)
    g3 = 0.8 + x1**-3 - x2

    cntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16],
                      colors='black')
    ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True)

    cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown')
    cg1.set(path_effects=[patheffects.withTickedStroke(angle=135)])

    cg2 = ax.contour(x1, x2, g2, [0], colors='orangered')
    cg2.set(path_effects=[patheffects.withTickedStroke(angle=60, length=2)])

    cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')
    cg3.set(path_effects=[patheffects.withTickedStroke(spacing=7)])

    ax.set_xlim(0, 4)
    ax.set_ylim(0, 4)

    plt.show()


.. _sphx_glr_download_gallery_images_contours_and_fields_contours_in_optimization_demo.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: contours_in_optimization_demo.ipynb <contours_in_optimization_demo.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: contours_in_optimization_demo.py <contours_in_optimization_demo.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: contours_in_optimization_demo.zip <contours_in_optimization_demo.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
