
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/roi_demo.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        Click :ref:`here <sphx_glr_download_examples_roi_demo.py>`
        to download the full example code

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

.. _sphx_glr_examples_roi_demo.py:


Working with RegionOfInterest objects
=====================================

.. GENERATED FROM PYTHON SOURCE LINES 6-44

.. code-block:: default


    import matplotlib.pyplot as plt
    import numpy as np
    from neo.core import CircularRegionOfInterest, RectangularRegionOfInterest, PolygonRegionOfInterest
    from numpy.random import rand


    def plot_roi(roi, shape):
        img = rand(120, 100)
        pir = np.array(roi.pixels_in_region()).T
        img[pir[1], pir[0]] = 5

        plt.imshow(img, cmap='gray_r')
        plt.clim(0, 5)

        ax = plt.gca()
        ax.add_artist(shape)


    roi = CircularRegionOfInterest(x=50.3, y=50.8, radius=30.2)
    shape = plt.Circle(roi.centre, roi.radius, color='r', fill=False)
    plt.subplot(1, 3, 1)
    plot_roi(roi, shape)

    roi = RectangularRegionOfInterest(x=50.3, y=40.2, width=40.1, height=50.3)
    shape = plt.Rectangle((roi.x - roi.width/2.0, roi.y - roi.height/2.0),
                          roi.width, roi.height, color='r', fill=False)
    plt.subplot(1, 3, 2)
    plot_roi(roi, shape)

    roi = PolygonRegionOfInterest(
        (20.3, 30.2), (80.7, 30.1), (55.2, 59.4)
    )
    shape = plt.Polygon(np.array(roi.vertices), closed=True, color='r', fill=False)
    plt.subplot(1, 3, 3)
    plot_roi(roi, shape)

    plt.show()


.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_examples_roi_demo.py:

.. only:: html

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


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

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

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

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


.. only:: html

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

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