.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/data_visualization/spectra_yscale.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_data_visualization_spectra_yscale.py: Plotting spectra with different y-axis scales ============================================= The :func:`~.api.plot.plot_spectra` function supports the ``yscale`` argument to plot spectra with any of the y-axis scales provided by :mod:`matplotlib.scale`: in addition to the commonly used ``'linear'`` and ``'log'`` scales, the ``'symlog'``, ``'asinh'``, ``'logit'``, ``'function'`` and ``'functionlog'`` scales are also available with the ``'overlap'``, ``'cascade'`` and ``'mosaic'`` styles. The parameters specific to each scale (for example ``linthresh`` for the ``'symlog'`` scale) are passed as keyword arguments, following the :meth:`matplotlib.axes.Axes.set_yscale` API. With the ``'heatmap'`` style, the ``yscale`` argument is passed as the ``norm`` argument of :meth:`~.api.signals.Signal2D.plot`. .. GENERATED FROM PYTHON SOURCE LINES 20-22 First, we simulate a few spectra with values spanning several orders of magnitude, as typically obtained with, for example, EELS or CL data. .. GENERATED FROM PYTHON SOURCE LINES 22-38 .. code-block:: Python import numpy as np import hyperspy.api as hs rng = np.random.default_rng(1) wavelength = np.linspace(400, 900, 350) spectra = [] for amplitude, decay in [(120, 80), (80, 150), (40, 250)]: data = amplitude * np.exp(-(wavelength - 400) / decay) + rng.poisson( 0.2, wavelength.size ) spectra.append(hs.signals.Signal1D(data)) spectra[-1].axes_manager[0].name = "Wavelength" spectra[-1].axes_manager[0].units = "nm" .. GENERATED FROM PYTHON SOURCE LINES 39-41 By default, the y-axis uses a ``'linear'`` scale, which hides the detail of the low-intensity features. .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: Python hs.plot.plot_spectra(spectra, style="overlap") .. image-sg:: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_001.png :alt: spectra yscale :srcset: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 45-47 The same data plotted with a logarithmic y-axis scale makes the low-intensity part of the spectra readable. .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python hs.plot.plot_spectra(spectra, style="overlap", yscale="log") .. image-sg:: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_002.png :alt: spectra yscale :srcset: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 51-54 The ``'symlog'`` scale is useful when the data spans several orders of magnitude around zero: the ``linthresh`` argument defines the range (``-linthresh`` to ``linthresh``) within which the scale is linear. .. GENERATED FROM PYTHON SOURCE LINES 54-59 .. code-block:: Python spectra_shifted = [s - s.data.mean() for s in spectra] hs.plot.plot_spectra(spectra_shifted, style="overlap", yscale="symlog", linthresh=10) .. image-sg:: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_003.png :alt: spectra yscale :srcset: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 60-63 Alternatively, the ``'asinh'`` scale is similar to the ``'symlog'`` scale, but with a smoother transition between the linear and logarithmic regions, the width of which is set with the ``linear_width`` argument. .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: Python hs.plot.plot_spectra(spectra_shifted, style="overlap", yscale="asinh", linear_width=20) .. image-sg:: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_004.png :alt: spectra yscale :srcset: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 67-70 Custom scales can also be used with the ``'function'`` and ``'functionlog'`` scales by passing the ``functions`` argument: in this case, the y-axis follows the square of the values. .. GENERATED FROM PYTHON SOURCE LINES 70-84 .. code-block:: Python def forward(x): return x**2 def inverse(x): return x ** (1 / 2) hs.plot.plot_spectra( spectra, style="overlap", yscale="function", functions=(forward, inverse) ) .. image-sg:: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_005.png :alt: spectra yscale :srcset: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 85-87 The ``'cascade'`` style supports the ``'linear'``, ``'log'`` and ``'symlog'`` scales. .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: Python hs.plot.plot_spectra(spectra, style="cascade", yscale="log") .. image-sg:: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_006.png :alt: spectra yscale :srcset: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 91-94 Finally, when plotting with the ``'heatmap'`` style, the ``yscale`` argument is passed as the ``norm`` argument of :meth:`~.api.signals.Signal2D.plot`. .. GENERATED FROM PYTHON SOURCE LINES 94-96 .. code-block:: Python hs.plot.plot_spectra(spectra, style="heatmap", yscale="log") .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_007.png :alt: spectra yscale :srcset: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_007.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_008.png :alt: Stack of Signal :srcset: /auto_examples/data_visualization/images/sphx_glr_spectra_yscale_008.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/7 [00:00 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.261 seconds) .. _sphx_glr_download_auto_examples_data_visualization_spectra_yscale.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: spectra_yscale.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: spectra_yscale.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: spectra_yscale.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_