milia.plot.style#

Matplotlib house style for publication figures.

SciencePlots’ science style through twiga’s configure_matlib_style, with these overrides: serif type (Times New Roman) set through LaTeX, 10 pt labels, thin rules, no grid, no legend frame and the project palette. Figures are authored one panel at a time at the size they will occupy (panel()) and saved as PDF, so LaTeX composes the sub-figures.

LaTeX is on by default; call configure(latex=False) on a machine without TeX. With LaTeX on, any text from data must go through texsafe().

Module Attributes

TEXTWIDTH

Text width of the target document, in inches (455.24 pt).

RC

The overrides layered on twiga's base style.

GAP

Space between the x-axis label and a legend placed below it, in points.

Functions

configure([latex, size])

Apply the house style globally.

filebackend()

Switch to matplotlib's non-interactive Agg backend.

finish(ax)

Left and bottom spines only, ticks pointing out, no minor ticks.

golden([width])

Height of a golden-ratio figure of a given width.

legend(ax[, ncol, handles, labels])

Legend centred below the axes; place() moves it just under the x-axis label after layout.

pad(*arrays[, frac, floor])

Axis limits around every array, padded by a share of the range.

panel([fraction, aspect])

figsize for a panel occupying a share of the text width.

place(fig)

Put every legend() just under its axes' x-axis label, in as many columns as fit.

save(fig, path[, png])

Save a figure as PDF (and a 600 dpi PNG preview), tightly cropped, then close it.

show(fig)

Lay out the figure and its legends as save() does, then display it.

sketch([latex, scale, length, randomness])

The house style with hand-drawn paths, for illustrations rather than results.

style([latex])

The house style for the duration of a block, restoring the previous settings afterwards.

texsafe(text)

Escape text for LaTeX; a no-op when text.usetex is off.

milia.plot.style.TEXTWIDTH = 6.3#

Text width of the target document, in inches (455.24 pt).

milia.plot.style.RC: dict[str, Any] = {'axes.edgecolor': '#222222', 'axes.grid': False, 'axes.labelcolor': '#222222', 'axes.labelsize': 10, 'axes.linewidth': 0.5, 'axes.prop_cycle': cycler('color', ['#107591', '#00c0bf', '#f69a48', '#fdcd49', '#8da798', '#a19368', '#525252', '#a6761d', '#7035b7', '#cf166e']), 'figure.dpi': 150, 'font.family': 'serif', 'font.serif': ['Times New Roman', 'DejaVu Serif'], 'font.size': 10, 'legend.fontsize': 10, 'legend.frameon': False, 'legend.title_fontsize': 10, 'lines.linewidth': 1.0, 'mathtext.fontset': 'dejavuserif', 'savefig.bbox': 'tight', 'savefig.pad_inches': 0.05, 'text.color': '#222222', 'xtick.color': '#222222', 'xtick.labelsize': 10, 'ytick.color': '#222222', 'ytick.labelsize': 10}#

The overrides layered on twiga’s base style.

milia.plot.style.golden(width=6.3)#

Height of a golden-ratio figure of a given width.

Parameters:

width (float) – Figure width, in inches.

Return type:

float

Returns:

width * 0.618.

Examples

>>> golden(10.0)
6.18
milia.plot.style.panel(fraction=0.48, aspect=0.618)#

figsize for a panel occupying a share of the text width.

Authoring each panel at the size it will finally occupy keeps type the same size across a composed figure; scaling panels in LaTeX does not.

Parameters:
  • fraction (float) – Share of the text width: 0.48 for two panels side by side, 0.31 for three, 1.0 for a full-width panel.

  • aspect (float) – Height as a multiple of width.

Return type:

tuple[float, float]

Returns:

(width, height) in inches.

Examples

>>> [round(v, 3) for v in panel(0.5, aspect=1.0)]
[3.15, 3.15]
milia.plot.style.configure(latex=True, size=12.0)#

Apply the house style globally.

Calls twiga’s configure_matlib_style (SciencePlots’ science style and the LaTeX toolchain) and layers RC on top.

Parameters:
  • latex (bool) – Typeset text through LaTeX; set False on a machine without TeX.

  • size (float) – Base font size passed to twiga.

Return type:

None

Examples

>>> configure(latex=False)
>>> import matplotlib as mpl
>>> mpl.rcParams["font.family"]
['serif']
milia.plot.style.style(latex=True, **overrides)#

The house style for the duration of a block, restoring the previous settings afterwards.

Parameters:
  • latex (bool) – Typeset text through LaTeX.

  • **overrides (Any) – Extra rc settings applied inside the block.

Yields:

Nothing; the style is active inside the block.

Examples

>>> import matplotlib as mpl
>>> with style(latex=False, **{"lines.linewidth": 3.0}):
...     mpl.rcParams["lines.linewidth"]
3.0
milia.plot.style.sketch(latex=True, scale=1.2, length=130, randomness=3, **overrides)#

The house style with hand-drawn paths, for illustrations rather than results.

Only the paths wobble; text stays in the house serif.

Parameters:
  • latex (bool) – Typeset text through LaTeX.

  • scale (float) – Wobble amplitude, in points.

  • length (float) – Wobble wavelength, in points.

  • randomness (float) – Jitter of the wavelength, in percent.

  • **overrides (Any) – Extra rc settings.

Yields:

Nothing; the style is active inside the block.

Examples

>>> import matplotlib as mpl
>>> with sketch(latex=False):
...     mpl.rcParams["path.sketch"]
(1.2, 130.0, 3.0)
milia.plot.style.filebackend()#

Switch to matplotlib’s non-interactive Agg backend.

For scripts that only save figures: an interactive backend fails on a Python built without Tcl/Tk, as uv installs it. Notebooks should not call it.

Examples

>>> filebackend()
Return type:

None

milia.plot.style.pad(*arrays, frac=0.04, floor=1e-09)#

Axis limits around every array, padded by a share of the range.

A deliberate margin, so a point at the extreme does not sit on the spine.

Parameters:
  • *arrays (Any) – Every array the axis will show.

  • frac (float) – Padding as a share of the range.

  • floor (float) – Smallest padding.

Return type:

tuple[float, float] | None

Returns:

(low, high), or None when there is nothing finite to pad.

Examples

>>> pad([0.0, 1.0])
(-0.04, 1.04)
>>> pad([]) is None
True
milia.plot.style.texsafe(text)#

Escape text for LaTeX; a no-op when text.usetex is off.

Use on any string from data placed in a figure while LaTeX is on.

Parameters:

text (str) – The text.

Return type:

str

Returns:

The escaped text.

Examples

>>> import matplotlib as mpl
>>> mpl.rcParams["text.usetex"] = False
>>> texsafe("a_b & c")
'a_b & c'
milia.plot.style.finish(ax)#

Left and bottom spines only, ticks pointing out, no minor ticks.

Parameters:

ax (Axes) – The axes.

Return type:

Axes

Returns:

The axes.

Examples

>>> import matplotlib.pyplot as plt
>>> ax = finish(plt.subplots()[1])
>>> ax.spines["top"].get_visible()
False
milia.plot.style.GAP = 3.0#

Space between the x-axis label and a legend placed below it, in points.

milia.plot.style.legend(ax, ncol=3, handles=None, labels=None)#

Legend centred below the axes; place() moves it just under the x-axis label after layout.

Parameters:
  • ax (Axes) – The axes.

  • ncol (int) – Columns, reduced by place() if the legend is wider than the figure.

  • handles (list | None) – Legend handles; the axes’ own by default.

  • labels (list | None) – Legend labels.

Return type:

None

Examples

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> _ = ax.plot([0, 1], label="x")
>>> legend(ax)
>>> ax.get_legend() is not None
True
milia.plot.style.place(fig)#

Put every legend() just under its axes’ x-axis label, in as many columns as fit.

Called by save() and show() after layout.

Parameters:

fig (Figure) – The figure.

Return type:

None

Examples

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> _ = ax.plot([0, 1], label="x")
>>> legend(ax)
>>> place(fig)
milia.plot.style.show(fig)#

Lay out the figure and its legends as save() does, then display it.

Parameters:

fig (Figure) – The figure.

Return type:

None

Examples

>>> import matplotlib.pyplot as plt
>>> show(plt.subplots()[0])
milia.plot.style.save(fig, path, png=True)#

Save a figure as PDF (and a 600 dpi PNG preview), tightly cropped, then close it.

Parameters:
  • fig (Figure) – The figure.

  • path (str | Path) – Target path; the suffix is replaced.

  • png (bool) – Also write the PNG preview.

Return type:

Path

Returns:

The PDF path.

Examples

>>> import tempfile, matplotlib.pyplot as plt
>>> from pathlib import Path
>>> fig, ax = plt.subplots()
>>> save(fig, Path(tempfile.mkdtemp()) / "figure", png=False).suffix
'.pdf'