Documentation

Module contents

nbfigtulz.config module

nbfigtulz.config.config: Dict[str, Any] = {'dpi': 300, 'img_dir': 'img', 'size_large': (8.0, 6.0), 'size_small': (4.0, 3.0), 'suppress_pgf': True, 'thumbnail_quality': 42, 'thumbnail_scale': 0.5}

Configuration used in nbfigtulz.figtools.save_fig.

  • img_dir: The directory where the generated images are stored. This directory is not created by nbfigtulz and has to exist before nbfigtulz.figtools.save_fig is called for the first time.

  • size_small: Default width and height of small images in units of inches.

  • size_large: Default width and height of large images in units of inches.

  • dpi: DPI used to store images.

  • thumbnail_scale: The thumbnail can be scaled w.r.t. the generated PNG to save space. Setting the scale to, e.g., 0.5 shrinks the PNG by a factor of 2.

  • thumbnail_quality: The image quality of the thumbnail, on a scale from 1 (worst) to 95 (best).

  • suppress_pgf: Suppress PGF rendering by default.

nbfigtulz.figtools module

class nbfigtulz.figtools.FigContext(backend: str = '', rcParams: Optional[Dict[str, Any]] = None)

Bases: object

Context manager that temporally overwrites matplotlib.rcParams and the backend.

Parameters
  • backend – The new backend, defaults to "pgf".

  • rcParams – The temporal rcParams, defaults to lualatex for pgf.texsystem.

class nbfigtulz.figtools.Size(value)

Bases: enum.Enum

Common image sizes.

This is nothing but a convenient wrapper for the image sizes stored inside of nbfigtulz.config.config. Possible values are Size.SMALL and Size.LARGE.

LARGE = 2

Refers to size_large in nbfigtulz.config.config.

SMALL = 1

Refers to size_small in nbfigtulz.config.config.

get_size() Tuple[float, float]

Returns the image size as a tuple of width and height.

Returns

The width and height as a tuple of floats in units of inches.

nbfigtulz.figtools.img_grid(images: Tuple[nbfigtulz.thumbnail.Thumbnail, ...], *, n_columns: Optional[int] = None, width: Optional[int] = None) IPython.core.display.HTML

Arranges images in a grid.

Parameters
  • images – List of images.

  • n_columns – Number of columns, defaults to number of images.

  • width – If not None the width of the grid in units of pixels.

Returns

The image grid.

nbfigtulz.figtools.save_fig(fig: Any, filename_base: str, resize: Any = Size.SMALL, *, suppress_pgf: Optional[bool] = None, quiet: bool = False, thumbnail_scale: Optional[float] = None, tight_layout: bool = True, thumbnail_bkg: Optional[Tuple[int, int, int]] = None, **kwargs) nbfigtulz.thumbnail.Thumbnail

The provided figure is stored to disk in the PNG and PGF (optional) format.

Parameters
  • fig – A matplotlib.pyplot.figure instance.

  • filename_base – Base name (w/o file type suffix) of the PNG and PGF file.

  • resize – If not None this will resize the figure. Pass anything with a get_size() -> Tuple[float, float] member function (e.g., Size) or a tuple of two floats. Those floats are interpreted as the new width and height in units of inches, respectively.

  • suppress_pgf – Suppress the generation of the PGF file.

  • quiet – Do not print the FQNs of the generated files.

  • thumbnail_scale – If not None this overwrites the default thumbnail scaling in nbfigtulz.config.config.

  • tight_layout – Require tight layout.

  • thumbnail_bkg – Background color of thumbnail as RGB tuple. (Default: (255, 255, 255) aka white.)

  • kwargs – Arguments passed to matplotlib.pyplot.savefig.

Returns

The rendered PNG image.

nbfigtulz.figtools.with_context(func: Optional[Callable] = None, rcParams: Optional[Dict[str, Any]] = None, preamble: Optional[Tuple[str, ...]] = None) Callable

Wraps a function call inside the FigContext context.

Parameters
  • func – A function.

  • rcParams – Temporal rcParams for FigContext.

  • preamble – List of commands for rcParams['pgf.preamble'].

Returns

The wrapped function.

nbfigtulz.thumbnail module

class nbfigtulz.thumbnail.Thumbnail(bytes: Any, filename: str, thumbnail_scale: float, thumbnail_quality: int, bkg_color: Tuple[int, int, int], width: Optional[int] = None, print_compression: bool = False)

Bases: object

Wrapper for a PNG image.

Parameters
  • bytes – Bytes of the PNG image.

  • filename – The file name.

  • thumbnail_scale – Scaling factor for thumbnail.

  • thumbnail_quality – Image quality of the thumbnail.

  • bkg_color – Background color as RGB tuple.

  • width – If not None the image is rescaled to the given width in units of pixels.

  • print_compression – Print the compression rate of the thumbnail w.r.t. the PNG.

to_html(link: bool = True) str

Renders the image into an HTML image tag.

Parameters

link – Whether to make the image tag a downloadable file link.

Returns

An HTML image tag.