Plotting

Starting from v0.5.4, there is now a function rampy.plot_spectrum to plot a spectrum with Plotly. It is a simple function that takes as input the X and Y values of a spectrum and plot it. It can also take as input the baselines and smoothed signals to plot them too. Have a look at the Example notebooks for further example of use.

class rampy.plotting.plot_spectrum(x, y, fig=None, baselines=None, baseline_labels=None, smoothed_signals=None, smoothed_labels=None, label='Spectrum', color=None, roi=None, xaxis_title=None, yaxis_title=None, title=None)

Bases:

Plots a spectrum with optional baselines, smoothed signals, and regions of interest (bir) using Plotly.

This function adds a single spectrum to a Plotly figure. It supports overlaying baselines, smoothed signals, and highlighting regions of interest (bir). If no figure is provided, a new Plotly figure is created.

Parameters:
  • x (array-like) – The x-axis values for the spectrum.

  • y (array-like) – The y-axis values for the spectrum.

  • fig (plotly.graph_objects.Figure, optional) – The Plotly figure to which the spectrum will be added. If None, a new figure will be created. Default is None.

  • baselines (list of array-like, optional) – List of baseline arrays to overlay on the spectrum. Default is None.

  • baseline_labels (list of str, optional) – Labels for each baseline. Default is None.

  • smoothed_signals (list of array-like, optional) – List of smoothed signal arrays to overlay on the spectrum. Default is None.

  • smoothed_labels (list of str, optional) – Labels for each smoothed signal. Default is None.

  • label (str, optional) – Label for the spectrum. Default is “Spectrum”.

  • color (str or tuple, optional) – Base color for the spectrum. If None, a default color will be chosen. Default is None.

  • bir (list or ndarray, optional) – Regions of interest to highlight on the plot. Should be a list or array of shape (n_regions, 2), where each row specifies the start and end of a region (e.g., [[x_start1, x_end1], [x_start2, x_end2]]). Default is None.

  • xaxis_title (str, optional) – Title for the x-axis. If None, no specific title is set. Default is None.

  • yaxis_title (str, optional) – Title for the y-axis. If None, no specific title is set. Default is None.

  • title (str, optional) – Title for the entire plot. If None, no title is set. Default is None.

Returns:

The updated Plotly figure object containing the plotted spectrum.

Return type:

plotly.graph_objects.Figure

Examples

Plot a simple spectrum:

>>> import numpy as np
>>> import plotly.graph_objects as go
>>> x = np.linspace(0, 10, 500)
>>> y = np.sin(x) + np.random.normal(0, 0.1, len(x))
>>> fig = plot_spectrum(
        x=x,
        y=y,
        label="Sample Spectrum",
        xaxis_title="Wavenumber (cm⁻¹)",
        yaxis_title="Intensity (a.u.)",
        title="Sample Spectrum Analysis"
    )
>>> fig.show()

Plot a spectrum with baseline and smoothed signal:

>>> baseline = 0.5 * np.ones_like(x)
>>> smoothed = np.convolve(y - baseline, np.ones(10)/10, mode='same')
>>> fig = plot_spectrum(
>>>        x=x,
>>>        y=y,
>>>        baselines=[baseline],
>>>        baseline_labels=["Constant Baseline"],
>>>        smoothed_signals=[smoothed],
>>>        smoothed_labels=["Moving Average"],
>>>        xaxis_title="Wavenumber (cm⁻¹)",
>>>        yaxis_title="Intensity (a.u.)"
>>>    )
>>> fig.show()

Notes

  • All input arrays are automatically converted to vectors using np.asarray(...).flatten() to ensure compatibility with Plotly.

  • Baselines are displayed as dashed lines with distinct colors.

  • Smoothed signals are displayed as dotted lines with distinct colors.

  • Regions of interest (BIR) are highlighted with semi-transparent yellow rectangles.

  • The function returns the figure object, allowing for further customization if needed.