fig_savers

Module Contents

class fig_savers.FigureSaver(save_under: Optional[pocketutils.core.PathLike] = None, *, clear: Union[bool, Callable[[matplotlib.figure.Figure], Any]] = False, as_type: Optional[str] = None, check_paths: bool = True, sanitize_paths: bool = True, log: Optional[Callable[[str], None]] = None, kwargs: Mapping[str, Any] = None)

Offers some small, specific extensions over matplotlib’s figure.savefig.

Specifically::
  • can remove the figure from memory each iteration

  • creates directories as needed

  • can save a multi-figure PDF

  • complains about issues

  • can auto-fix some

  • the FigureSaver.save function handles iterators of different types

See save() for more info.

Constructor.

Parameters
  • save_under – A parent directory assumed for all saved figures

  • clear – Auto-close each figure after saving (if a callable, call it)

  • as_type – Force a specific filename suffix (e.g. as_type=".jpg")

  • check_paths – Check that paths are valid; without sanitize_paths, raises an error if a path is invalid; see pocketutils.tools.path_tools.PathTools.sanitize_path()

  • sanitize_paths – Try to sanitize paths (implies check_paths)

  • log – Call this function with a message indicating that a sanitized path differs

  • kwargs – Passed to savefig

save(figure: FigureSeqLike, path: pocketutils.core.PathLike = '', names: Optional[Iterator[str]] = None, *, overwrite: bool = True, use_labels: bool = False) None
Saves either:
  1. a single figure figure to path path

  2. a bunch of figures to directory path if figure is an iterable (list, dict, etc) over figures

  3. a single PDF with multiple figures, if path ends in .pdf

If figure is iterable (case 2), it can be either:
  • an iterable over Figures

  • an iterable over (name, Figure) pairs, where name provides the filename (under the directory path)

If it’s the first case and names is set, will use those to provide the filenames. Otherwise, falls back to numbering them (ex: directory/1.png, etc).

If use_labels is set, tries to use each Figure.get_label().

Note

Figure labels are not typically set by default; you should call Figure.set_label() when using use_labels.

save_all_as_pdf(figures: FigureSeqLike, path: pocketutils.core.PathLike, names: Optional[Iterator[str]] = None, *, overwrite: bool = True, metadata: Optional[Mapping[str, str]] = None) None

Save a single PDF with potentially many figures. See save() for more info.

save_all(figures: FigureSeqLike, directory: pocketutils.core.PathLike = '.', names: Optional[Iterator[str]] = None, *, use_labels: bool = False, overwrite: bool = True) None

Saves potentially multiple figures into a directory.

See save() for more info.

save_one(figure: matplotlib.figure.Figure, path: pocketutils.core.PathLike, overwrite: bool) None

Saves one figure to a specific path. This is pretty similar to Figure.savefig.