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

    Click :ref:`here <sphx_glr_download_gallery_misc_bbox_intersect.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_misc_bbox_intersect.py:


===========================================
Changing colors of lines intersecting a box
===========================================

The lines intersecting the rectangle are colored in red, while the others
are left as blue lines. This example showcases the `intersect_bbox` function.





.. image:: /gallery/misc/images/sphx_glr_bbox_intersect_001.png
    :class: sphx-glr-single-img





.. code-block:: python


    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.transforms import Bbox
    from matplotlib.path import Path

    # Fixing random state for reproducibility
    np.random.seed(19680801)


    left, bottom, width, height = (-1, -1, 2, 2)
    rect = plt.Rectangle((left, bottom), width, height, facecolor="#aaaaaa")

    fig, ax = plt.subplots()
    ax.add_patch(rect)

    bbox = Bbox.from_bounds(left, bottom, width, height)

    for i in range(12):
        vertices = (np.random.random((2, 2)) - 0.5) * 6.0
        path = Path(vertices)
        if path.intersects_bbox(bbox):
            color = 'r'
        else:
            color = 'b'
        ax.plot(vertices[:, 0], vertices[:, 1], color=color)

    plt.show()


.. _sphx_glr_download_gallery_misc_bbox_intersect.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
