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

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

.. _sphx_glr_gallery_color_color_by_yvalue.py:


================
Color by y-value
================

Use masked arrays to plot a line with different colors by y-value.

.. GENERATED FROM PYTHON SOURCE LINES 8-25

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    t = np.arange(0.0, 2.0, 0.01)
    s = np.sin(2 * np.pi * t)

    upper = 0.77
    lower = -0.77

    supper = np.ma.masked_where(s < upper, s)
    slower = np.ma.masked_where(s > lower, s)
    smiddle = np.ma.masked_where((s < lower) | (s > upper), s)

    fig, ax = plt.subplots()
    ax.plot(t, smiddle, t, slower, t, supper)
    plt.show()




.. image-sg:: /gallery/color/images/sphx_glr_color_by_yvalue_001.png
   :alt: color by yvalue
   :srcset: /gallery/color/images/sphx_glr_color_by_yvalue_001.png, /gallery/color/images/sphx_glr_color_by_yvalue_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 26-39

.. admonition:: References

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

   - `matplotlib.axes.Axes.plot` / `matplotlib.pyplot.plot`

.. tags::

   styling: color
   styling: conditional
   plot-type: line
   level: beginner


.. _sphx_glr_download_gallery_color_color_by_yvalue.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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