.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/region_of_interest/combine_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_combine_PolygonROI.py: Combine PolygonROI ================== Combine several :class:`~.api.roi.PolygonROI`. .. 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 ROIs, here :class:`~.api.roi.PolygonROI`: .. GENERATED FROM PYTHON SOURCE LINES 17-20 .. code-block:: Python roi = hs.roi.PolygonROI([(2, 4.5), (4.5, 4.5), (4.5, 2), (3.5, 3.5)]) roi2 = hs.roi.PolygonROI([(0.5, 0.5), (1.2, 0.2), (1.5, 1), (0.2, 1.4)]) .. GENERATED FROM PYTHON SOURCE LINES 21-22 We plot the signal add the ROIs to the figure using :meth:`~hyperspy.roi.BaseInteractiveROI.add_widget`. .. GENERATED FROM PYTHON SOURCE LINES 22-28 .. code-block:: Python s.plot() roi.add_widget(s, axes=s.axes_manager.signal_axes) roi2.add_widget(s, axes=s.axes_manager.signal_axes) .. image-sg:: /auto_examples/region_of_interest/images/sphx_glr_combine_PolygonROI_001.png :alt: Signal :srcset: /auto_examples/region_of_interest/images/sphx_glr_combine_PolygonROI_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 29-31 Now that we have two ROIs, ``roi`` and ``roi2``, we can combine them to slice a signal by using the following function: .. GENERATED FROM PYTHON SOURCE LINES 31-35 .. code-block:: Python s_roi_combined = hs.roi.combine_rois(s, [roi, roi2]) s_roi_combined.plot() .. image-sg:: /auto_examples/region_of_interest/images/sphx_glr_combine_PolygonROI_002.png :alt: Signal :srcset: /auto_examples/region_of_interest/images/sphx_glr_combine_PolygonROI_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 36-39 It is also possible to get a boolean mask from the ROIs, which can be useful for interacting with other libraries. You need to supply the signal's ``axes_manager`` to get the correct parameters for creating the mask: .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: Python boolean_mask = hs.roi.mask_from_rois([roi, roi2], s.axes_manager) boolean_mask = hs.signals.Signal2D(boolean_mask) boolean_mask.plot() .. image-sg:: /auto_examples/region_of_interest/images/sphx_glr_combine_PolygonROI_003.png :alt: Signal :srcset: /auto_examples/region_of_interest/images/sphx_glr_combine_PolygonROI_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.653 seconds) .. _sphx_glr_download_auto_examples_region_of_interest_combine_PolygonROI.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: combine_PolygonROI.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: combine_PolygonROI.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: combine_PolygonROI.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_