cellspec.pl.spectrum_from_df

cellspec.pl.spectrum_from_df#

cellspec.pl.spectrum_from_df(spectrum_df, normalize=False, palette=None, title=None, outpath=None, figsize=None, dpi=600, ylabel=None, ylim=None, transparent=False)#

Plot mutation spectrum directly from a DataFrame.

Automatically detects format based on shape: - If shape is (n_samples, 96): treats as per-cell format (cells × contexts) - If shape is (96, n_groups): treats as per-group format (contexts × groups) - If shape is (96,): treats as single spectrum

Parameters:
  • spectrum_df (pd.DataFrame or pd.Series) – Mutation spectrum DataFrame or Series: - Series with 96 trinuc contexts: plots single spectrum - DataFrame (cells × 96 contexts): plots multiple spectra, one row per cell - DataFrame (96 contexts × groups): plots multiple spectra, one row per group

  • normalize (bool, default False) – Normalize to proportions (sum to 1)

  • palette (str, list, dict, or None, optional) – Color palette (see spc.pl.spectrum for details)

  • title (str, optional) – Plot title

  • outpath (str, optional) – Full output path for saving plot (e.g., ‘results/spectrum.png’). Parent directory will be created if it doesn’t exist. If None, plot is displayed but not saved.

  • figsize (tuple, optional) – Figure size. If None, auto-calculated based on number of spectra

  • dpi (int, default 600) – DPI for saved figure

  • ylabel (str, optional) – Y-axis label. If None, uses “Mutation count” or “Proportion” based on normalize

  • ylim (tuple of (float, float), optional) – Y-axis limits as (ymin, ymax). If None, automatically calculated from data.

  • transparent (bool, default False) – Save figure with transparent background (only affects saved files)

Return type:

None

Examples

>>> import cellspec as spc
>>> import pandas as pd
>>>
>>> # Plot a single spectrum (Series)
>>> spectrum = adata.obsm["spectrum_somatic"].sum(axis=0)
>>> spc.pl.spectrum_from_df(spectrum, title="Total Somatic Mutations")
>>>
>>> # Save plot to a file
>>> spc.pl.spectrum_from_df(spectrum, outpath="results/spectrum.png")
>>>
>>> # Plot from custom DataFrame (96 contexts × groups)
>>> private_spectra = adata.uns["spectrum_private"]
>>> spc.pl.spectrum_from_df(private_spectra, title="Private Mutations", normalize=True)
>>>
>>> # Plot from subset of cells (cells × 96 contexts)
>>> cell_subset = adata.obsm["spectrum_somatic"].loc[["cell1", "cell2", "cell3"]]
>>> spc.pl.spectrum_from_df(cell_subset, title="Selected Cells")

Notes

This function automatically detects the DataFrame format and plots accordingly. For more control over grouping and aggregation from AnnData objects, use spc.pl.spectrum().