

.. _sphx_glr_gallery_api_span_regions.py:


================
Using span_where
================

Illustrate some helper functions for shading regions where a logical
mask is True

See :meth:`matplotlib.collections.BrokenBarHCollection.span_where`




.. image:: /gallery/api/images/sphx_glr_span_regions_001.png
    :align: center





.. code-block:: python

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.collections as collections


    t = np.arange(0.0, 2, 0.01)
    s1 = np.sin(2*np.pi*t)
    s2 = 1.2*np.sin(4*np.pi*t)


    fig, ax = plt.subplots()
    ax.set_title('using span_where')
    ax.plot(t, s1, color='black')
    ax.axhline(0, color='black', lw=2)

    collection = collections.BrokenBarHCollection.span_where(
        t, ymin=0, ymax=1, where=s1 > 0, facecolor='green', alpha=0.5)
    ax.add_collection(collection)

    collection = collections.BrokenBarHCollection.span_where(
        t, ymin=-1, ymax=0, where=s1 < 0, facecolor='red', alpha=0.5)
    ax.add_collection(collection)


    plt.show()

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



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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

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

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