
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/lines_bars_and_markers/curve_error_band.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_lines_bars_and_markers_curve_error_band.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_lines_bars_and_markers_curve_error_band.py:


=====================
Curve with error band
=====================

This example illustrates how to draw an error band around a parametrized curve.

A parametrized curve x(t), y(t) can directly be drawn using `~.Axes.plot`.

.. GENERATED FROM PYTHON SOURCE LINES 10-26

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.patches import PathPatch
    from matplotlib.path import Path

    N = 400
    t = np.linspace(0, 2 * np.pi, N)
    r = 0.5 + np.cos(t)
    x, y = r * np.cos(t), r * np.sin(t)

    fig, ax = plt.subplots()
    ax.plot(x, y, "k")
    ax.set(aspect=1)




.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_curve_error_band_001.png
   :alt: curve error band
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_curve_error_band_001.png, /gallery/lines_bars_and_markers/images/sphx_glr_curve_error_band_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 28-40

An error band can be used to indicate the uncertainty of the curve.
In this example we assume that the error can be given as a scalar *err*
that describes the uncertainty perpendicular to the curve in every point.

We visualize this error as a colored band around the path using a
`.PathPatch`. The patch is created from two path segments *(xp, yp)*, and
*(xn, yn)* that are shifted by +/- *err* perpendicular to the curve *(x, y)*.

Note: This method of using a `.PathPatch` is suited to arbitrary curves in
2D. If you just have a standard y-vs.-x plot, you can use the simpler
`~.Axes.fill_between` method (see also
:doc:`/gallery/lines_bars_and_markers/fill_between_demo`).

.. GENERATED FROM PYTHON SOURCE LINES 40-79

.. code-block:: Python



    def draw_error_band(ax, x, y, err, **kwargs):
        # Calculate normals via centered finite differences (except the first point
        # which uses a forward difference and the last point which uses a backward
        # difference).
        dx = np.concatenate([[x[1] - x[0]], x[2:] - x[:-2], [x[-1] - x[-2]]])
        dy = np.concatenate([[y[1] - y[0]], y[2:] - y[:-2], [y[-1] - y[-2]]])
        l = np.hypot(dx, dy)
        nx = dy / l
        ny = -dx / l

        # end points of errors
        xp = x + nx * err
        yp = y + ny * err
        xn = x - nx * err
        yn = y - ny * err

        vertices = np.block([[xp, xn[::-1]],
                             [yp, yn[::-1]]]).T
        codes = np.full(len(vertices), Path.LINETO)
        codes[0] = codes[len(xp)] = Path.MOVETO
        path = Path(vertices, codes)
        ax.add_patch(PathPatch(path, **kwargs))


    _, axs = plt.subplots(1, 2, layout='constrained', sharex=True, sharey=True)
    errs = [
        (axs[0], "constant error", 0.05),
        (axs[1], "variable error", 0.05 * np.sin(2 * t) ** 2 + 0.04),
    ]
    for i, (ax, title, err) in enumerate(errs):
        ax.set(title=title, aspect=1, xticks=[], yticks=[])
        ax.plot(x, y, "k")
        draw_error_band(ax, x, y, err=err,
                        facecolor=f"C{i}", edgecolor="none", alpha=.3)

    plt.show()




.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_curve_error_band_002.png
   :alt: constant error, variable error
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_curve_error_band_002.png, /gallery/lines_bars_and_markers/images/sphx_glr_curve_error_band_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 80-93

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.patches.PathPatch`
   - `matplotlib.path.Path`

.. tags::

   component: error
   plot-type: line
   level: intermediate


.. _sphx_glr_download_gallery_lines_bars_and_markers_curve_error_band.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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