.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/region_of_interest/PolygonROI.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_region_of_interest_PolygonROI.py: PolygonROI ========== Use a :class:`~.api.roi.PolygonROI` interactively on a :class:`~.api.signals.Signal2D`. .. GENERATED FROM PYTHON SOURCE LINES 9-11 .. code-block:: Python import hyperspy.api as hs .. GENERATED FROM PYTHON SOURCE LINES 12-13 Create a signal: .. GENERATED FROM PYTHON SOURCE LINES 13-15 .. code-block:: Python s = hs.data.atomic_resolution_image() .. GENERATED FROM PYTHON SOURCE LINES 16-17 Create the ROI, here a :class:`~.api.roi.PolygonROI`: .. GENERATED FROM PYTHON SOURCE LINES 17-19 .. code-block:: Python roi = hs.roi.PolygonROI() .. GENERATED FROM PYTHON SOURCE LINES 20-30 Initializing the ROI with no arguments puts you directly into constructing the polygon. Do this by clicking where you want the vertices. Click the first vertex to complete the polygon. You can reset the polygon by pressing "Esc" and you can move the entire polygon by shift-clicking and dragging. An alternative to define the :class:`~.api.roi.PolygonROI` interactively is to set the vertices of the ROI using the :attr:`~.api.roi.PolygonROI.vertices`. We can use :meth:`~hyperspy.roi.BaseInteractiveROI.interactive` to add the ROI to the figure and get the signal from the ROI. .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: Python s.plot() roi.vertices = [(2, 4.5), (4.5, 4.5), (4.5, 2), (3, 3)] s_roi = roi.interactive(s, axes=s.axes_manager.signal_axes) .. image-sg:: /auto_examples/region_of_interest/images/sphx_glr_PolygonROI_001.png :alt: Signal :srcset: /auto_examples/region_of_interest/images/sphx_glr_PolygonROI_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 36-37 Then we can extract the ROI from the signal and plot it. .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: Python s_roi.plot() .. image-sg:: /auto_examples/region_of_interest/images/sphx_glr_PolygonROI_002.png :alt: Signal :srcset: /auto_examples/region_of_interest/images/sphx_glr_PolygonROI_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 41-44 The signal will contain a lot of NaNs, so take this into consideration when doing further processing. E.g. use :meth:`~.api.signals.BaseSignal.nanmean` instead of :meth:`~.api.signals.BaseSignal.mean`. .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: Python mean_value = s_roi.nanmean(axis=(0,1)).data[0] print("Mean value in ROI:", mean_value) .. rst-class:: sphx-glr-script-out .. code-block:: none Mean value in ROI: 5.207270455522169 .. GENERATED FROM PYTHON SOURCE LINES 49-51 In some cases, it is easier to choose the area to remove rather than keep. By using the ``inverted`` parameter, everything except the ROI will be retained: .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: Python s_roi_inv = roi(s, inverted=True) s_roi_inv.plot() .. image-sg:: /auto_examples/region_of_interest/images/sphx_glr_PolygonROI_003.png :alt: Signal :srcset: /auto_examples/region_of_interest/images/sphx_glr_PolygonROI_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.637 seconds) .. _sphx_glr_download_auto_examples_region_of_interest_PolygonROI.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: PolygonROI.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: PolygonROI.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: PolygonROI.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_