.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/examples/general/plot_sorted_data.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_generated_examples_general_plot_sorted_data.py: Sorting Data ------------ Some types of characterization data, such as FTIR and XPS, are typically expressed with their x-values in decending order. Rather than having to ensure users rearrange their data into ascending order before using pybaselines, the :class:`~.Baseline` object handles this internally and returns values in the same order as the input x-values. This is especially important if other parameters from the selected baseline method are desired such as weights, since those are likewise returned in the same order as the input x-values. .. GENERATED FROM PYTHON SOURCE LINES 15-63 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from pybaselines import Baseline from pybaselines.utils import gaussian x = np.linspace(500, 4000, 1000) signal = ( + gaussian(x, 8, 650, 16) + gaussian(x, 9, 1100, 50) + gaussian(x, 8, 1350, 20) + gaussian(x, 11, 2800, 20) + gaussian(x, 8, 2900, 20) + gaussian(x, 5, 3400, 120) ) baseline = 0.08 + 0.00004 * x + gaussian(x, 3, 3500, 1000) noise = np.random.default_rng(0).normal(0, 0.1, len(x)) y = signal + baseline + noise # reverse both x and y x_reversed = x[::-1] y_reversed = y[::-1] lam = 1e6 fit, params = Baseline(x).iarpls(y, lam=lam) fit_reversed, params_reversed = Baseline(x_reversed).iarpls(y_reversed, lam=lam) _, (ax_1, ax_2) = plt.subplots(nrows=2, sharex=True) ax_1.plot(x, y) ax_1.plot(x, fit) ax_2.plot(x_reversed, y_reversed) ax_2.plot(x_reversed, fit_reversed) ax_2.invert_xaxis() ax_2.set_xlabel(r'Wavenumber (cm$^{-1}$)') ax_1.set_ylabel('Absorbance (%)') ax_2.set_ylabel('Absorbance (%)') ax_1.set_title('Sorted Data') ax_2.set_title('Reversed Data') print( 'Outputs same for sorted and unsorted baselines: ', np.allclose(fit, fit_reversed[::-1], rtol=1e-14, atol=1e-14) ) .. image-sg:: /generated/examples/general/images/sphx_glr_plot_sorted_data_001.png :alt: Sorted Data, Reversed Data :srcset: /generated/examples/general/images/sphx_glr_plot_sorted_data_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Outputs same for sorted and unsorted baselines: True .. GENERATED FROM PYTHON SOURCE LINES 65-67 Likewise, relevant output parameters such as weights are also sorted to match the input x-values. .. GENERATED FROM PYTHON SOURCE LINES 67-86 .. code-block:: Python _, (ax_1, ax_2) = plt.subplots(nrows=2, sharex=True) ax_1.plot(x, y) ax_1.plot(x, fit) ax_2.plot(x, params['weights'], 'ro', label='sorted') ax_2.plot(x_reversed, params_reversed['weights'], 'b.', label='reversed') ax_2.invert_xaxis() ax_2.legend() ax_2.set_xlabel(r'Wavenumber (cm$^{-1}$)') ax_1.set_ylabel('Absorbance (%)') ax_2.set_ylabel('Weights') print( 'Weights same for sorted and unsorted baselines: ', np.allclose(params['weights'], params_reversed['weights'][::-1], rtol=1e-14, atol=1e-14) ) plt.show() .. image-sg:: /generated/examples/general/images/sphx_glr_plot_sorted_data_002.png :alt: plot sorted data :srcset: /generated/examples/general/images/sphx_glr_plot_sorted_data_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Weights same for sorted and unsorted baselines: True .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.192 seconds) .. _sphx_glr_download_generated_examples_general_plot_sorted_data.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sorted_data.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sorted_data.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_sorted_data.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_