Data visualisation

RamanSPy provides a variety of data visualisation tools which can be used to plot different types of Raman spectroscopic data and analysis results. This includes the visualisation of (collections of) spectra, imaging and volumetric data, relevant statistics (e.g. mean spectra, peak intensity distributions) and more. These can also be used to visualise analysis results, such as endmembers, fractional abundances, clusters, projections and others.

The visualisation tools are implemented as functions which can be accessed via the ramanspy.plot module.

Spectra

ramanspy.plot.spectra(spectra: NDArray | SpectralObject | List[NDArray | SpectralObject] | List[List[NDArray | SpectralObject]], wavenumber_axis: NDArray = None, *, plot_type: _core.SPECTRA_PLOT_TYPES = 'separate', title: str = 'Raman spectra', xlabel: str = 'Raman shift (cm$^{{{-1}}}$)', ylabel: str = 'Intensity (a.u.)', label: List[str] = None, color=None, **kwargs)[source]

Visualising spectra.

Parameters:
  • spectra (Union[NDArray, SpectralObject, List[Union[NDArray, SpectralObject]], List[List[Union[NDArray, SpectralObject]]]]) – The spectral data to plot. Can plot a single spectrum and collection(s) of spectra, where SpectralObject := Union[SpectralContainer, Spectrum, SpectralImage, SpectralVolume].

  • wavenumber_axis (numpy.ndarray, optional) – The shift axis of the data provided. Only used if spectra contains data which is not a spectral container. Must match for all spectra provided.

  • plot_type ({"single", "separate", "stacked", "single stacked"}, optional) –

    The type of the plot. Default is 'separate'.

    • 'single' - groups are plotted in the same plot;

    • ``’separate’` - groups are plotted in individual plots;

    • 'stacked' - groups are plotted in individual plots, stacked on top of each other;

    • 'single stacked' - groups are plotted in the same plot, stacked on top of each other.

  • title (str, optional) – The title of the plot. Default is 'Raman spectra'.

  • xlabel (str, optional) – The x-axis label of the plot. Default is 'Raman shift (cm$^{{{-1}}}$)'.

  • ylabel (str, optional) – The y-axis label of the plot. Default is 'Intensity (a.u.)'.

  • label (Union[str, List[str]], optional) – The label(s) of the spectral group(s) provided. Must match spectra. Default is None, i.e. no labels.

  • color (Union[str, List[str]], optional) – The color(s) to use for each plot. Default is None, i.e. the default matplotlib’s colormap will be used, which is the veridis colormap.

  • **kwargs – Additional parameters. Will be passed to the matplotlib.pyplot.plot method.

Returns:

The Axes object(s) of the plot(s).

Return type:

matplotlib.axes.Axes or List[matplotlib.axes.Axes]

Examples

import ramanspy as rp

# plots a single spectrum
rp.plot.spectra(spectrum)

# plots all spectra within a SpectralContainer instance as a single group
rp.plot.spectra(spectral_object)

# plots 3 spectra as individual groups
rp.plot.spectra([spectrum_1, spectrum_2, spectrum_3])

# plots 2 groups of spectra (size does not need to be the same across groups)
rp.plot.spectra([spectrum_1, spectrum_2, spectrum_3], [spectrum_4, spectrum_5])

# plots 3 spectra as a single group
rp.plot.spectra([[spectrum_1, spectrum_2, spectrum_3]])

See also

Check the Visualising spectra tutorial for more information about how to visualise spectra.

ramanspy.plot.mean_spectra(spectra: NDArray | SpectralObject | List[NDArray | SpectralObject] | List[List[NDArray | SpectralObject]], wavenumber_axis: NDArray = None, *, plot_type: _core.SPECTRA_PLOT_TYPES = 'separate', dist: bool = True, title: str = 'Raman spectra', xlabel: str = 'Raman shift (cm$^{{{-1}}}$)', ylabel: str = 'Intensity (a.u.)', label: List[str] = None, color=None, **kwargs)[source]

Visualising spectral distributions.

Parameters:
  • spectra (Union[NDArray, SpectralObject, List[Union[NDArray, SpectralObject]], List[List[Union[NDArray, SpectralObject]]]]) – The spectral data to plot. Can plot a single spectrum and collection(s) of spectra, where SpectralObject := Union[SpectralContainer, Spectrum, SpectralImage, SpectralVolume].

  • wavenumber_axis (numpy.ndarray, optional) – The shift axis of the data provided. Only used if spectra contains data which is not a spectral container. Must match for all spectra provided.

  • plot_type ({"single", "separate", "stacked", "single stacked"}, optional) –

    The type of the plot. Default is 'separate'.

    • 'single' - groups are plotted in the same plot;

    • ``’separate’` - groups are plotted in individual plots;

    • 'stacked' - groups are plotted in individual plots, stacked on top of each other;

    • 'single stacked' - groups are plotted in the same plot, stacked on top of each other.

  • dist (bool, optional) – If dist=True, the method will plot the mean spectrum and a 95% confidence interval of each distribution (default). If dist=False, it will plot the mean and the individual spectra comprising each distribution.

  • title (str, optional) – The title of the plot. Default is 'Raman spectra'.

  • xlabel (str, optional) – The x-axis label of the plot. Default is 'Raman shift (cm$^{{{-1}}}$)'.

  • ylabel (str, optional) – The y-axis label of the plot. Default is 'Intensity (a.u.)'.

  • label (Union[str, List[str]], optional) – The label(s) of the spectral group(s) provided. Default is None, i.e. no labels.

  • color (Union[str, List[str]], optional) – The color(s) to use for each plot. Default is None, i.e. the default matplotlib’s colormap will be used, which is the veridis colormap.

  • **kwargs

    Additional parameters. Will be passed to the matplotlib.pyplot.plot method.

Returns:

The Axes object(s) of the plot(s).

Return type:

matplotlib.axes.Axes or List[matplotlib.axes.Axes]

Examples

import ramanspy as rp

# plots the distributions of 2 groups of spectra
rp.plot.mean_spectra([spectrum_1, spectrum_2, spectrum_3], [spectrum_4, spectrum_5])

See also

Check the Visualising spectral distributions tutorial for more information about how to visualise spectral distributions.

ramanspy.plot.peaks(spectrum: Spectrum, *, title: str = 'Raman spectra', xlabel: str = 'Raman shift (cm$^{{{-1}}}$)', ylabel: str = 'Intensity (a.u.)', color=None, height=None, threshold=None, distance=None, prominence=None, width=None, wlen=None, rel_height=0.5, plateau_size=None, return_peaks: bool = False, **plt_kwargs)[source]

Visualising peaks.

Parameters:
  • spectrum (Spectrum) – The spectral data to plot.

  • title (str, optional) – The title of the plot. Default is 'Raman spectra'.

  • xlabel (str, optional) – The x-axis label of the plot. Default is 'Raman shift (cm$^{{{-1}}}$)'.

  • ylabel (str, optional) – The y-axis label of the plot. Default is 'Intensity (a.u.)'.

  • color (matplotlib color, optional) – The color(s) to use for each plot. Default is None, i.e. the default matplotlib’s colormap will be used, which is the veridis colormap.

  • height (optional) – Parameter for the scipy.signal.find_peaks method. Check the original documentation for more information.

  • threshold (optional) –

    Parameter for the scipy.signal.find_peaks method. Check the original documentation for more information.

  • distance (optional) –

    Parameter for the scipy.signal.find_peaks method. Check the original documentation for more information.

  • prominence (optional) –

    Parameter for the scipy.signal.find_peaks method. Check the original documentation for more information.

  • width (optional) –

    Parameter for the scipy.signal.find_peaks method. Check the original documentation for more information.

  • wlen (optional) –

    Parameter for the scipy.signal.find_peaks method. Check the original documentation for more information.

  • rel_heigh (optional) –

    Parameter for the scipy.signal.find_peaks method. Check the original documentation for more information.

  • plateau_size (optional) –

    Parameter for the scipy.signal.find_peaks method. Check the original documentation for more information.

  • return_peaks (bool, optional) – Whether to return the peaks and their properties. Default is False.

  • **plt_kwargs

    Additional parameters. Will be passed to the matplotlib.pyplot.plot method.

Returns:

  • matplotlib.axes.Axes – The Axes object of the plot.

  • numpy.ndarray, optional – The peaks found in the spectrum. Only returned if return_peaks is True.

  • dict, optional – The properties of the peaks found in the spectrum. Only returned if return_peaks is True.

Examples

import ramanspy as rp

# plots peaks within a single spectrum
rp.plot.peaks(spectrum, **kwargs)

See also

Check the Visualising peaks tutorial for more information about how to visualise the peaks of spectra.

Image

ramanspy.plot.image(image: ndarray[Any, dtype[_ScalarType_co]] | List[ndarray[Any, dtype[_ScalarType_co]]], *, ax=None, threshold: Number | List[Number] = None, title: str | List[str] = 'Raman image', xlabel: str | List[str] = None, ylabel: str | List[str] = None, cbar: bool | List[bool] = True, cbar_label: str | List[str] = 'Peak intensity', color=None, **plt_kwargs)[source]

Visualising imaging Raman data.

If more than one image slice is provided, they will be plotted in separate plots using the corresponding parameters given.

Parameters:
  • image (Union[NDArray, List[NDArray]]) – 2D array(s) corresponding to the spectral slice(s) to visualise.

  • threshold (Union[Number, List[Number]], optional) – If provided, all values less than the given threshold will be discarded, i.e. set to the minimum value in the data.

  • title (Union[str, List[str]], optional) – The plot title(s) to use for each plot. Default is 'Raman image'.

  • xlabel (Union[str, List[str]], optional) – The x-axis label(s) to use for each plot. Default is None, i.e. no label.

  • ylabel (Union[str, List[str]], optional) – The y-axis label(s) to use for each plot. Default is None, i.e. no label.

  • cbar (Union[bool, List[bool]], optional) – Whether to include a colorbar or not in each plot. Default is True.

  • cbar_label (Union[str, List[str]], optional) – If cbar=True, the colorbar label(s) to use for each plot. Default is 'Peak intensity'.

  • color (Union[Matplotlib color, List[Matplotlib color]], optional) – The color(s) to use for each plot. Default is None, i.e. the default matplotlib’s colormap will be used, which is the veridis colormap.

  • **plt_kwargs (keyword arguments, optional) – Additional parameters. Will be passed to the matplotlib.pyplot.imshow method. Each parameter can be given by single instance or as a list of instances for each plot.

Returns:

The Axes object(s) of the plot(s).

Return type:

matplotlib.axes.Axes or List[matplotlib.axes.Axes]

Examples

import ramanspy as rp

# plot single image slice
ax = rp.plot.image(raman_image.band(1500))

# visualising
plt.show()

# saving
ax.figure.savefig('...')

# plot a list of image slices
ax = rp.plot.image([raman_image.band(1500), raman_image.band(2500)])

# plot a list of image slices with shared parameters
ax = rp.plot.image([raman_image.band(1500), raman_image.band(2500)], title="Spectral slice", cbar=True, ...)

# plot a list of image slices with different parameters
ax = rp.plot.image([raman_image.band(1500), raman_image.band(2500)], title=["Spectral slice A", "Spectral slice B"], cbar=[True, False], ...)

See also

Check the Visualising imaging data tutorial for more information about how to visualise imaging data.

Volumetric

ramanspy.plot.volume(volume: ndarray[Any, dtype[_ScalarType_co]] | List[ndarray[Any, dtype[_ScalarType_co]]], *, ax=None, threshold: Number = None, color=None, title: str | List[str] = 'Raman volume', xlabel: str | List[str] = None, ylabel: str | List[str] = None, zlabel: str | List[str] = None, cbar: bool | List[bool] = True, cbar_label: str | List[str] = 'Peak intensity', **plt_kwargs)[source]

Visualising volumetric Raman data.

If more than one volume slice is provided, they will be plotted in separate plots using the corresponding parameters given.

Parameters:
  • volume (Union[NDArray, List[NDArray]]) – 3D array(s) corresponding to the spectral slice(s) to visualise.

  • threshold (Union[Number, List[Number]], optional) – If provided, all values less than the given threshold will be discarded, i.e. set to the minimum value in the data.

  • title (Union[str, List[str]], optional) – The plot title(s) to use for each plot. Default is 'Raman volume'.

  • xlabel (Union[str, List[str]], optional) – The x-axis label(s) to use for each plot. Default is None, i.e. no label.

  • ylabel (Union[str, List[str]], optional) – The y-axis label(s) to use for each plot. Default is None, i.e. no label.

  • zlabel (Union[str, List[str]], optional) – The z-axis label(s) to use for each plot. Default is None, i.e. no label.

  • cbar (Union[bool, List[bool]], optional) – Whether to include a colorbar or not in each plot. Default is True.

  • cbar_label (Union[str, List[str]], optional) – If cbar=True, the colorbar label(s) to use for each plot. Default is 'Peak intensity'.

  • color (Union[Matplotlib color, List[Matplotlib color]], optional) – The color(s) to use for each plot. Default is None, i.e. the default matplotlib’s colormap will be used, which is the veridis colormap.

  • **plt_kwargs (keyword arguments, optional) – Additional parameters. Will be passed to the matplotlib.pyplot.scatter method. Each parameter can be given by single instance or as a list of instances for each plot.

Returns:

The Axes object(s) of the plot(s).

Return type:

matplotlib.axes.Axes or List[matplotlib.axes.Axes]

Examples

import ramanspy as rp

# plot single volume slice
ax = rp.plot.volume(raman_volume.band(1500))

# visualising
plt.show()

# saving
ax.figure.savefig('...')

# plot a list of volume slices
rp.plot.volume([raman_volume.band(1500), raman_volume.band(2500)])

# plot a list of volume slices with shared parameters
rp.plot.volume([raman_volume.band(1500), raman_volume.band(2500)], title="Spectral slice", cbar=True, ...)

# plot a list of volume slices with different parameters
rp.plot.volume([raman_volume.band(1500), raman_volume.band(2500)], title=["Spectral slice A", "Spectral slice B"], cbar=[True, False], ...)

See also

Check the Visualising volumetric data tutorial for more information about how to visualise volumetric data.

Other

Peak distributions

ramanspy.plot.peak_dist(spectra: SpectralObject | List[SpectralObject] | List[List[SpectralObject]], band: Number, *, ax=None, labels: List[str] = None, title: str = 'Peak distribution', ylabel: str = 'Intensity (a.u.)', **kwargs)[source]

Visualising peak distributions as barplots.

Error bars represent one standard deviation of uncertainty.

Parameters:
  • spectra (Union[SpectralObject, List[SpectralObject], List[List[SpectralObject]]]) – The spectral data to plot, where SpectralObject := Union[SpectralContainer, Spectrum, SpectralImage, SpectralVolume].

  • band (Number) – The spectral band of interest.

  • ax (Matplotlib Axes object, optional) – If provided, the plot will be added to the given Axes instance. Default is None, i.e. a new Axes instance will be created.

  • labels (Union[str, List[str]], optional) – The label(s) of the spectral group(s) provided. Must match spectra. Default is None, i.e. no labels.

  • title (str, optional) – The title of the plot. Default is 'Raman spectra'.

  • ylabel (str, optional) – The y-axis label of the plot. Default is 'Intensity (a.u.)'.

  • **kwargs – Additional parameters. Will be passed to the matplotlib.pyplot.bar method.

Returns:

The Axes object of the plot.

Return type:

matplotlib.axes.Axes

Examples

import matplotlib.pyplot as plt
import ramanspy as rp

# plots the peak distributions at 1500cm^-1 of 2 groups of spectra
ax = rp.plot.peak_dist([spectrum_1, spectrum_2, spectrum_3], [spectrum_4, spectrum_5], band=1500, labels=["Group A", "Group B"])

# visualising
plt.show()  # or rp.plot.show()

# saving
ax.figure.savefig('...')

See also

Check the Visualising peak distributions tutorial for more information about how to visualise peak distributions.