Plotting with FoodSpec¶
This page summarizes common plot types in FoodSpec and the helper functions to generate them. All helpers return Matplotlib Axes and use simple defaults suitable for scientific reporting.
For notation see the Glossary. Pair every plot with the quantitative metric it supports (see Metrics & Evaluation).
What?¶
Catalog of visualization helpers for spectra, embeddings, classification/regression diagnostics, ratios, and hyperspectral maps.
Why?¶
Spectral interpretation benefits from paired visuals and metrics: overlays to check preprocessing, PCA/t-SNE to view structure (with silhouette/between-within metrics), classification/regression plots to diagnose errors and agreement, and ratio plots to show chemistry-driven separations.
When?¶
Use after preprocessing/features are fixed, and when summarizing CV/test results or exploratory structure. Limitations: visuals are qualitative unless paired with metrics; avoid over-interpreting small-n plotsâadd CIs/bootstraps where possible.
Spectra overlays and means¶
from foodspec.viz import plot_spectra_overlay, plot_mean_with_ci
ax = plot_spectra_overlay(spectra, wavenumbers, labels=sample_ids)
ax = plot_mean_with_ci(spectra, wavenumbers, group_labels=groups, ci=95)
PCA and embedding plots¶
from foodspec.viz import plot_pca_scores, plot_pca_loadings
ax = plot_pca_scores(scores, labels=classes, components=(1, 2))
ax = plot_pca_loadings(loadings, wavenumbers, components=(1, 2))
Classification diagnostics¶
from foodspec.viz import plot_confusion_matrix, plot_roc_curve
ax = plot_confusion_matrix(cm, class_labels, normalize=True)
ax = plot_roc_curve(fpr, tpr, auc_value)
Correlation and heatmaps¶
from foodspec.viz import plot_correlation_heatmap
ax = plot_correlation_heatmap(corr_matrix, labels=feature_names)
Ratio plots¶
from foodspec.viz import plot_ratio_by_group, plot_ratio_scatter, plot_ratio_vs_continuous
ax1 = plot_ratio_by_group(ratios, group_labels, kind="box")
ax2 = plot_ratio_scatter(ratio_x, ratio_y, group_labels)
ax3 = plot_ratio_vs_continuous(ratios, cont_var)
Hyperspectral maps¶
from foodspec.viz.hyperspectral import plot_hyperspectral_intensity_map
ax = plot_hyperspectral_intensity_map(cube, target_wavenumber=1655, window=5)
Regression calibration and agreement¶
from foodspec.viz import (
plot_regression_calibration,
plot_calibration_with_ci,
plot_bland_altman,
)
ax = plot_regression_calibration(y_true, y_pred)
ax = plot_calibration_with_ci(y_true, y_pred)
ax = plot_bland_altman(y_true, y_pred)
Reproducible figure generation¶
- Embeddings:
python docs/examples/visualization/generate_embedding_figures.pyâ PCA scores/loadings and t-SNE with accompanying metrics. - Mixtures/NNLS: (describe or run) a synthetic or example-oils script to save mixture overlay + residuals.
- Ratios: reuse example oils to create ratio-by-group and ratio-vs-continuous plots.
- Classification/regression: run simple PCA+SVM or PLS calibration examples and call the viz helpers above.
Where these appear in workflows¶
- Oil authentication: confusion matrix, PCA scores/loadings, boxplots of ratios.
- Heating: ratio vs time plots (
viz.heating.plot_ratio_vs_time), correlation heatmaps. - Mixtures/calibration: regression calibration plots with CI; residual overlays.
- QC: confusion matrices and spectra overlays for suspect vs reference.
- Hyperspectral: intensity/ratio maps; see Hyperspectral mapping.