Classes
SpectraPlot
Visualisation of NMR spectra from a DataFrame.
Columns are ppm values (or numeric column names); rows are samples. Supports overlay, mean-with-SD-envelope, stacked, and single-spectrum plots.
Parameters: spectra (pd.DataFrame): DataFrame with rows=samples and columns=ppm values (float column names) or any numeric column names interpretable as ppm. ppm (list or np.ndarray, optional): Explicit ppm axis. If None, inferred from ``spectra.columns``. label (pd.Series or list, optional): Group labels per sample used for colour-coding. Length must match ``len(spectra)``. color_dict (dict, optional): Mapping ``{group: color}`` for each unique label value. Auto-generated from Plotly's qualitative palette when None.
Examples: >>> import pandas as pd >>> import numpy as np >>> from metbit.viz.spectra import SpectraPlot >>> ppm = np.linspace(0.5, 9.5, 500) >>> spectra = pd.DataFrame(np.random.rand(20, 500), columns=ppm) >>> label = pd.Series(["A"]*10 + ["B"]*10) >>> sp = SpectraPlot(spectra, label=label) >>> fig = sp.overlay() >>> fig.show()
Methods
__init__(self, spectra: pd.DataFrame, ppm: Optional[Union[list, np.ndarray]]=None, label: Optional[Union[pd.Series, list]]=None, color_dict: Optional[dict]=None)
overlay(self, alpha: float=0.5, linewidth: float=1.0, fig_height: int=500, fig_width: int=1400, font_size: int=14, title: Optional[str]=None, xaxis_title: str='δ¹H (ppm)', yaxis_title: str='Intensity', xaxis_reversed: bool=True)
Overlay all spectra on a single plot, coloured by label.
Parameters: alpha (float): Line opacity. Default is 0.5. linewidth (float): Line width in pixels. Default is 1.0. fig_height (int): Figure height in pixels. Default is 500. fig_width (int): Figure width in pixels. Default is 1400. font_size (int): Base font size. Default is 14. title (str, optional): Plot title. Default is None. xaxis_title (str): X-axis label. Default is ``"delta1H (ppm)"``. yaxis_title (str): Y-axis label. Default is ``"Intensity"``. xaxis_reversed (bool): Reverse the x-axis (NMR convention). Default is True.
Returns: go.Figure: Plotly figure with all spectra overlaid.
Examples: >>> fig = sp.overlay(alpha=0.4, linewidth=0.8) >>> fig.show()
mean_sd(self, fig_height: int=500, fig_width: int=1400, font_size: int=14, title: Optional[str]=None, show_individual: bool=False, xaxis_title: str='δ¹H (ppm)', yaxis_title: str='Intensity', xaxis_reversed: bool=True)
Plot mean spectrum with shaded SD envelope per group.
For each group (or for all samples when no label is set), a solid mean line and a shaded standard-deviation band are drawn using ``go.Scatter`` with ``fill="tonexty"``.
Parameters: fig_height (int): Figure height in pixels. Default is 500. fig_width (int): Figure width in pixels. Default is 1400. font_size (int): Base font size. Default is 14. title (str, optional): Plot title. Default is None. show_individual (bool): When True, individual spectra are drawn as faint background lines. Default is False. xaxis_title (str): X-axis label. Default is ``"delta1H (ppm)"``. yaxis_title (str): Y-axis label. Default is ``"Intensity"``. xaxis_reversed (bool): Reverse the x-axis (NMR convention). Default is True.
Returns: go.Figure: Plotly figure with mean and SD envelope traces.
Examples: >>> fig = sp.mean_sd(show_individual=True) >>> fig.show()
stacked(self, offset_factor: float=0.3, fig_height: int=800, fig_width: int=1400, font_size: int=12, title: Optional[str]=None, xaxis_title: str='δ¹H (ppm)', xaxis_reversed: bool=True)
Plot spectra stacked vertically with a uniform offset.
Each spectrum is shifted upward by ``offset_factor * max(|spectra|)`` relative to the previous one so they do not overlap. Tick labels on the y-axis show sample IDs.
Parameters: offset_factor (float): Fraction of the global intensity range used as the vertical step between spectra. Default is 0.3. fig_height (int): Figure height in pixels. Default is 800. fig_width (int): Figure width in pixels. Default is 1400. font_size (int): Base font size. Default is 12. title (str, optional): Plot title. Default is None. xaxis_title (str): X-axis label. Default is ``"delta1H (ppm)"``. xaxis_reversed (bool): Reverse the x-axis (NMR convention). Default is True.
Returns: go.Figure: Plotly figure with stacked spectrum traces.
Examples: >>> fig = sp.stacked(offset_factor=0.5) >>> fig.show()
single(self, sample_id: Optional[Union[int, str]]=None, annotate_peaks: Optional[List[float]]=None, fig_height: int=400, fig_width: int=1400, font_size: int=14, title: Optional[str]=None, xaxis_title: str='δ¹H (ppm)', yaxis_title: str='Intensity', xaxis_reversed: bool=True)
Plot a single NMR spectrum.
When ``sample_id`` is None the median spectrum (element-wise median across all samples) is plotted. Vertical dashed lines with ppm annotations can be added via ``annotate_peaks``.
Parameters: sample_id (int or str, optional): Index label or integer position of the sample to plot. When None the median spectrum is used. Default is None. annotate_peaks (list of float, optional): ppm positions at which vertical dashed annotation lines are drawn. Default is None. fig_height (int): Figure height in pixels. Default is 400. fig_width (int): Figure width in pixels. Default is 1400. font_size (int): Base font size. Default is 14. title (str, optional): Plot title. Default is None. xaxis_title (str): X-axis label. Default is ``"delta1H (ppm)"``. yaxis_title (str): Y-axis label. Default is ``"Intensity"``. xaxis_reversed (bool): Reverse the x-axis (NMR convention). Default is True.
Returns: go.Figure: Plotly figure containing the single spectrum trace.
Examples: >>> fig = sp.single(sample_id=0, annotate_peaks=[1.33, 3.05]) >>> fig.show()