.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_ImportingData/plot_05_indexing_dnpdata_objects.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_01_ImportingData_plot_05_indexing_dnpdata_objects.py: .. _plot_05_indexing_dnpdata_objects: ============================================== How to select a slice from a 2D dnpdata object ============================================== This example demonstrates how to select a slice of a DNPData object. You can index a DNPData object by specifying the name of the dimension and the index of the slice. .. GENERATED FROM PYTHON SOURCE LINES 14-17 Import DNPLab and create a set of data -------------------------------------- Use the lorentzian function to generate a 2d set of lorentzian distributions. .. GENERATED FROM PYTHON SOURCE LINES 17-30 .. code-block:: Python import numpy as np from matplotlib.pylab import * import dnplab as dnp x = np.r_[-50:50:1024j] y = np.r_[-10:10:1] values = dnp.math.lineshape.lorentzian(x.reshape(-1, 1), y.reshape(1, -1), 0.5) data = dnp.DNPData(values, ["f2", "sample"], [x, y]) .. GENERATED FROM PYTHON SOURCE LINES 31-32 To specify a slice based on the index, we use an integer. This will select the slice at index 3. .. GENERATED FROM PYTHON SOURCE LINES 32-38 .. code-block:: Python data_slice_integer = data["sample", 3] # get slice by index # Taking the slice does not remove the sample dimension. We can remove dimensions of length 1 with the squeeze method data_slice_integer.squeeze() # remove "sample" dimension .. GENERATED FROM PYTHON SOURCE LINES 39-40 In many cases, we want the slice at a specific value of the coordinates. To do this, we use a float to specify the slice location. In python, by adding a period after the number, python interprets the number as a float instead of integer. .. GENERATED FROM PYTHON SOURCE LINES 40-46 .. code-block:: Python data_slice_float = data[ "sample", 3.0 ] # get slice at index closest to coordinates value of 3. data_slice_float.squeeze() # again, we remove the "sample" dimension. .. GENERATED FROM PYTHON SOURCE LINES 47-50 Plot the result --------------- Let's plot the 1d slices: .. GENERATED FROM PYTHON SOURCE LINES 50-58 .. code-block:: Python figure() dnp.plot(data_slice_integer) dnp.plot(data_slice_float) xlabel("Frequency (Hz)") ylabel("Signal (a.u.)") tight_layout() .. image-sg:: /auto_examples/01_ImportingData/images/sphx_glr_plot_05_indexing_dnpdata_objects_001.png :alt: plot 05 indexing dnpdata objects :srcset: /auto_examples/01_ImportingData/images/sphx_glr_plot_05_indexing_dnpdata_objects_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-60 Similarly, we can also slice by specifying a range of values. To do this, we use a tuple specify the minimum and maximum values for the index. .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python data_slice_range = data["sample", (-3, 3)] .. GENERATED FROM PYTHON SOURCE LINES 64-65 For an advanced tutorial how indexing works and how to extract individual data slice from a multi-dimensional dnpData object see the :ref:`plot_02_extract_data` tutorial. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.100 seconds) .. _sphx_glr_download_auto_examples_01_ImportingData_plot_05_indexing_dnpdata_objects.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_indexing_dnpdata_objects.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_indexing_dnpdata_objects.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_