sizing

Adjusting the size of matplotlib figures.

When preparing complex documents consisting of text and visual elements, it is often crucial to:

  1. Fit a figure in a specific region of the document.

  2. Avoid any rescaling of the figure upon inclusion, so that its fixed size components (e.g. text elements) are preserved.

To this aim,

  1. It might be convenient to use the size of other layout elements of the document (line width, slide height, etc.) as length units.

  2. It is necessary to ensure that a figure is sized accurately.

This module provides helper functions to achieve both these goals. Precisely,

  1. The function fig_size() allows to use the values (in inches) in rcParams['figure.figsize'] (default: [6.4, 4.8]) as custom width and height units, converting a length specified in these units to its value in inches (standard matplotlib’s unit). The latter can then be used as input elsewhere.

    When typesetting a TeX document, the size (in pt) of many layout elements can be assessed including the command \showthe\<somelength> and inspecting the compiler log; <somelength> is the name of the layout element (for a list of common possibilities see this table). To convert the output to inches, simply divide by 72.27.

  2. The function get_fig_size() retrives the save-time size of a figure (for a specific backend). Calling this function iteratively, set_fig_size() attemps to enforce a given save-time size for a figure, adjusting its draw-time size. Often only one of th

    Built-in support is available only for a handful of vector formats, listed in SUPPORTED_FORMATS. For other file formats, extra dependencies are required (see EXTRAS_FORMATS).

Attributes

SUPPORTED_FORMATS

Supported file formats by get_fig_size().

EXTRAS_FORMATS

Optional supported file formats by get_fig_size().

MAX_SIZING_ATTEMPTS

Maximum number of sizing attempts by set_fig_size().

SIZING_TOLERANCE

Relative tolerance of set_fig_size().

Functions

fig_size([width, height, ratio])

Converts rcParams['savefig.figsize'] units to inches.

get_fig_size(fig, **savefig_kw)

Measures the actual saved figure size.

set_fig_size(fig[, size, which])

Sets the actual saved figure size.