uclchem.style#

Publication-quality matplotlib style for UCLCHEM.

Applies STIX serif fonts and MNRAS-compatible sizes without requiring a LaTeX installation. The style is applied automatically when uclchem is imported. Set the environment variable UCLCHEM_NO_STYLE=1 before importing to suppress auto-apply.

Key Functions:

Example Usage:

>>> import uclchem  # style applied on import
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> _ = ax.set_xlabel(uclchem.style.format_chemical_formula("HCO+"))
>>> uclchem.style.reset()      # restore matplotlib defaults
>>> uclchem.style.apply()      # re-apply UCLCHEM style

Module Contents#

Functions#

apply(→ None)

Apply style to the global matplotlib rcParams.

context(→ collections.abc.Generator[None, None, None])

Apply style temporarily as a context manager.

format_chemical_formula(→ str)

Convert a UCLCHEM species name to a mathtext label.

format_reaction_label(→ str)

Convert a UCLCHEM reaction string to a mathtext legend label.

reset(→ None)

Restore matplotlib's built-in default rcParams.

Attributes#

uclchem.style.apply(style: dict[str, object] = UCLCHEM_STYLE) None[source]#

Apply style to the global matplotlib rcParams.

Parameters:

style (dict[str, object]) – Mapping of rcParam keys to values. Defaults to UCLCHEM_STYLE.

uclchem.style.context(style: dict[str, object] = UCLCHEM_STYLE) collections.abc.Generator[None, None, None][source]#

Apply style temporarily as a context manager.

Parameters:

style (dict[str, object]) – Mapping of rcParam keys to values. Defaults to UCLCHEM_STYLE.

Yields:

None – All plt calls inside the block use style; settings are restored on exit.

Examples

>>> import uclchem
>>> with uclchem.style.context():
...     pass  # plots here use UCLCHEM style
uclchem.style.format_chemical_formula(name: str) str[source]#

Convert a UCLCHEM species name to a mathtext label.

Subscripts digit runs and superscripts charge suffixes so the result renders correctly with matplotlib’s built-in mathtext engine (no LaTeX required).

Parameters:

name (str) – UCLCHEM species name, e.g. "HCO+", "#H2O", "@CO2", "E-", "o-H2".

Returns:

Mathtext string, e.g. r"$\mathrm{HCO^{+}}$".

Return type:

str

Examples

>>> format_chemical_formula("HCO+")
'$\\mathrm{HCO^{+}}$'
>>> format_chemical_formula("H2O")
'$\\mathrm{H_{2}O}$'
>>> format_chemical_formula("#H2O")
'#$\\mathrm{H_{2}O}$'
uclchem.style.format_reaction_label(rxn: str, k_mean: float | None = None) str[source]#

Convert a UCLCHEM reaction string to a mathtext legend label.

Parameters:
  • rxn (str) – Reaction string in UCLCHEM format, e.g. "H3+ + CO -> HCO+ + H2".

  • k_mean (float | None) – Mean rate coefficient to append to the label. If None no rate is shown. Default: None.

Returns:

Mathtext string suitable for use as a matplotlib legend label.

Return type:

str

Examples

>>> format_reaction_label("H2 + O -> OH + H")
'$\\mathrm{H_{2}}$ + $\\mathrm{O}$ $\\rightarrow$ $\\mathrm{OH}$ + $\\mathrm{H}$'
uclchem.style.reset() None[source]#

Restore matplotlib’s built-in default rcParams.

uclchem.style.UCLCHEM_STYLE: dict[str, object][source]#