Source code for sc_tools.pl.save
"""
Versioned figure saving: PDF + PNG with datetime prefix, dpi=300, under pdf/ and png/.
"""
from __future__ import annotations
from datetime import datetime
from pathlib import Path
import matplotlib.pyplot as plt
from ..utils.save import (
DEFAULT_FIGURE_DPI,
versioned_path,
)
[docs]
def save_figure(
fig: plt.Figure,
basename: str,
output_dir: str | Path,
dpi: int = DEFAULT_FIGURE_DPI,
dt: datetime | None = None,
bbox_inches: str = "tight",
pad_inches: float = 0.1,
create_pdf_png_folders: bool = True,
) -> tuple[Path, Path]:
"""
Save a figure in two formats with a versioned filename under pdf/ and png/.
Creates:
- output_dir/pdf/YYDDMM.hh.mm.basename.pdf
- output_dir/png/YYDDMM.hh.mm.basename.png
Parameters
----------
fig : matplotlib.figure.Figure
Figure to save.
basename : str
Base name (no extension), e.g. "volcano_faceted".
output_dir : str or Path
Root directory for figures (e.g. "figures/process_colocalization").
Subdirs "pdf" and "png" are created inside it.
dpi : int, optional
DPI for raster (PNG). Default 300.
dt : datetime, optional
Timestamp for version prefix; if None, uses now().
bbox_inches : str, optional
Passed to savefig. Default "tight".
pad_inches : float, optional
Passed to savefig. Default 0.1.
create_pdf_png_folders : bool, optional
If True (default), save under output_dir/pdf/ and output_dir/png/.
Returns
-------
tuple of Path
(path_to_pdf, path_to_png)
"""
output_dir = Path(output_dir)
if create_pdf_png_folders:
pdf_path = versioned_path(output_dir, basename, "pdf", subdir="pdf", dt=dt)
png_path = versioned_path(output_dir, basename, "png", subdir="png", dt=dt)
else:
pdf_path = versioned_path(output_dir, basename, "pdf", subdir=None, dt=dt)
png_path = versioned_path(output_dir, basename, "png", subdir=None, dt=dt)
save_kw = {"bbox_inches": bbox_inches, "pad_inches": pad_inches}
fig.savefig(pdf_path, **save_kw)
fig.savefig(png_path, dpi=dpi, **save_kw)
return pdf_path, png_path
__all__ = ["save_figure"]