Skip to content

Formatter API

The rwthplots.formatter module provides figure sizing utilities.

set_size() converts a typographic point width — or a named journal/paper preset — to a (width_in, height_in) tuple using the golden ratio for height. This ensures figures embed in LaTeX documents at exactly the right size without scaling, which would change the font size relative to the body text.

The conversion formula is:

width_in  = width_pt / 72.27
height_in = width_in × (√5 − 1) / 2 × (rows / cols)

All 18 named presets and their point widths are listed in Style Sheets — Size modifiers.

rwthplots.formatter.set_size

set_size(width: float | str, fraction=1, subplots=(1, 1))

Set figure dimensions to avoid scaling in LaTeX.

Parameters

width: float or string Document width in points, or a predefined name. Call list_presets() for all available names. fraction: float, optional Fraction of the width which you wish the figure to occupy subplots: array-like, optional The number of rows and columns of subplots. Returns


fig_dim: tuple Dimensions of figure in inches

Source code in src/rwthplots/formatter.py
def set_size(width: float | str, fraction=1, subplots=(1, 1)):
    """Set figure dimensions to avoid scaling in LaTeX.

    Parameters
    ----------
    width: float or string
            Document width in points, or a predefined name.
            Call ``list_presets()`` for all available names.
    fraction: float, optional
            Fraction of the width which you wish the figure to occupy
    subplots: array-like, optional
            The number of rows and columns of subplots.
    Returns
    -------
    fig_dim: tuple
            Dimensions of figure in inches
    """
    if isinstance(width, str):
        if width not in _PRESETS:  # type: ignore[operator]
            raise ValueError(
                f"Unknown predefined width {width!r}. "
                f"Known values: {sorted(_PRESETS)}. "
                "Pass a numeric point value for a custom width."
            )
        width_pt = _PRESETS[width]
    else:
        width_pt = width

    # Width of figure (in pts)
    fig_width_pt = width_pt * fraction
    # Convert from pt to inches
    inches_per_pt = 1 / 72.27

    # Golden ratio to set aesthetic figure height
    # https://disq.us/p/2940ij3
    golden_ratio = (5 ** .5 - 1) / 2

    # Figure width in inches
    fig_width_in = fig_width_pt * inches_per_pt
    # Figure height in inches
    fig_height_in = fig_width_in * golden_ratio * (subplots[0] / subplots[1])

    return fig_width_in, fig_height_in

rwthplots.formatter.list_presets

list_presets() -> dict[str, float]

Return all named width presets and their values in typographic points.

Source code in src/rwthplots/formatter.py
def list_presets() -> dict[str, float]:
    """Return all named width presets and their values in typographic points."""
    return dict(_PRESETS)