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:
spectraDataFrame with rows=samples and columns=ppm values(float column names) or any numeric column names interpretable as ppm.
ppmExplicit ppm axis. If None, inferredfrom ``spectra.columns``.
labelGroup labels per sample used forcolour-coding. Length must match ``len(spectra)``.
color_dictMapping ``{group: color}`` for each uniquelabel 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:
alphaLine opacity. Default is 0.5.linewidthLine width in pixels. Default is 1.0.fig_heightFigure height in pixels. Default is 500.fig_widthFigure width in pixels. Default is 1400.font_sizeBase font size. Default is 14.titlePlot title. Default is None.xaxis_titleX-axis label. Default is ``"delta1H (ppm)"``.yaxis_titleY-axis label. Default is ``"Intensity"``.xaxis_reversedReverse 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_heightFigure height in pixels. Default is 500.fig_widthFigure width in pixels. Default is 1400.font_sizeBase font size. Default is 14.titlePlot title. Default is None.show_individualWhen True, individual spectra are drawn asfaint background lines. Default is False.
xaxis_titleX-axis label. Default is ``"delta1H (ppm)"``.yaxis_titleY-axis label. Default is ``"Intensity"``.xaxis_reversedReverse 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_factorFraction of the global intensity range usedas the vertical step between spectra. Default is 0.3.
fig_heightFigure height in pixels. Default is 800.fig_widthFigure width in pixels. Default is 1400.font_sizeBase font size. Default is 12.titlePlot title. Default is None.xaxis_titleX-axis label. Default is ``"delta1H (ppm)"``.xaxis_reversedReverse 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_idIndex label or integer positionof the sample to plot. When None the median spectrum is used. Default is None.
annotate_peaksppm positions at whichvertical dashed annotation lines are drawn. Default is None.
fig_heightFigure height in pixels. Default is 400.fig_widthFigure width in pixels. Default is 1400.font_sizeBase font size. Default is 14.titlePlot title. Default is None.xaxis_titleX-axis label. Default is ``"delta1H (ppm)"``.yaxis_titleY-axis label. Default is ``"Intensity"``.xaxis_reversedReverse 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()