

.. _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.




.. image:: /gallery/color/images/sphx_glr_color_by_yvalue_001.png
    :align: center





.. code-block:: python

    import numpy as np
    import matplotlib.pyplot as plt

    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(np.logical_or(s < lower, s > upper), s)

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

**Total running time of the script:** ( 0 minutes  0.035 seconds)



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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

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

    `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
