rizer.misc.plt_utils#

Functions#

set_mpl_style(→ None)

Set the matplotlib style for plots.

get_text(x, y, text, ax[, color])

Add centered text with a consistent default bbox.

get_annotation(xy, xytext, text, ax[, color, ...])

Add a centered annotation with an arrow and a consistent default bbox.

save_current_figure(→ None)

Save the current figure to a file in the figures folder.

align_yaxis_np(→ None)

Align zeros of the two axes, zooming them out by same ratio.

align_zeros(→ None)

Align zeros of multiple axes, zooming them out by same ratio.

get_species_in_latex(→ str)

Return the LaTeX representation of a chemical species.

get_reaction_in_latex(→ str)

Return the LaTeX representation of a chemical reaction equation.

get_species_color(→ str)

Return a consistent color for a given chemical species.

Module Contents#

rizer.misc.plt_utils.set_mpl_style(nb_columns: int = 2) None#

Set the matplotlib style for plots.

Parameters:

nb_columns (int, optional) – Number of columns for the figure style. Options are 1 or 2. By default 2.

rizer.misc.plt_utils.get_text(x: float, y: float, text: str, ax: matplotlib.axes.Axes, color: Any | str | None = None)#

Add centered text with a consistent default bbox.

Parameters:
  • x (float) – Text coordinates.

  • y (float) – Text coordinates.

  • text (str) – Text to display.

  • ax (matplotlib.axes.Axes) – Axis to draw on.

  • color (Any or str or None, optional) – Text color. If None, reuse the color of the last line in the axis.

rizer.misc.plt_utils.get_annotation(xy: tuple[float, float], xytext: tuple[float, float], text: str, ax: matplotlib.axes.Axes, color: str | None = None, fontsize: float | None = None, arrow_lw: float | None = None, arrow_linestyle: str | None = None, arrow_relpos: tuple[float, float] | None = None, arrow_arrowstyle: str | None = None)#

Add a centered annotation with an arrow and a consistent default bbox.

Parameters:
  • xy (tuple of float) – Coordinates of the point being annotated.

  • xytext (tuple of float) – Coordinates where the text is placed.

  • text (str) – Text to display. Pass an empty string for arrow-only annotations.

  • ax (matplotlib.axes.Axes) – Axis to draw on.

  • color (str or None, optional) – Text and arrow color. If None, reuse the color of the last line in the axis.

  • fontsize (float or None, optional) – Font size override.

  • arrow_lw (float or None, optional) – Line width of the arrow.

  • arrow_linestyle (str or None, optional) – Line style of the arrow (e.g., "--").

  • arrow_relpos (tuple of float or None, optional) – Relative position of the arrow.

  • arrow_arrowstyle (str or None, optional) – Arrow style.

rizer.misc.plt_utils.save_current_figure(fig: matplotlib.figure.Figure, filename: str) None#

Save the current figure to a file in the figures folder.

Parameters:
rizer.misc.plt_utils.align_yaxis_np(ax1: matplotlib.axes.Axes, ax2: matplotlib.axes.Axes) None#

Align zeros of the two axes, zooming them out by same ratio.

Script taken from [SSO_shared_origin].

Parameters:

References

rizer.misc.plt_utils.align_zeros(axes: list[matplotlib.axes.Axes]) None#

Align zeros of multiple axes, zooming them out by same ratio.

Parameters:

axes (list of matplotlib.axes.Axes) – List of axes to align.

Notes

This function modifies the y-limits of the axes in place. It ensures that all axes have the same zero point and that the y-limits are adjusted to fit the data while maintaining the zero alignment.

References

https://stackoverflow.com/a/68869054

rizer.misc.plt_utils.get_species_in_latex(species: str) str#

Return the LaTeX representation of a chemical species.

Parameters:

species (str) – Chemical species as a string.

Returns:

LaTeX representation of the chemical species.

Return type:

str

Examples

>>> get_species_in_latex("O2")
'$\\mathregular{O_{2}}$'
>>> get_species_in_latex("CH4")
'$\\mathregular{CH_{4}}$'
>>> get_species_in_latex("CH4+")
'$\\mathregular{CH_{4}^{+}}$'
>>> get_species_in_latex("e-")
'$\\mathregular{e^{-}}$'
>>> get_species_in_latex("C2H6")
'$\\mathregular{C_{2}H_{6}}$'
>>> get_species_in_latex("C++")
'$\\mathregular{C^{2+}}$'
>>> get_species_in_latex("C(soot)")
'$\\mathregular{C}(\\mathregular{soot})$'
>>> get_species_in_latex("C(s)")
'$\\mathregular{C}(\\mathregular{s})$'
>>> get_species_in_latex("C+")
'$\\mathregular{C^{+}}$'
>>> get_species_in_latex("C++")
'$\\mathregular{C^{2+}}$'
>>> get_species_in_latex("C+++")
'$\\mathregular{C^{3+}}$'
rizer.misc.plt_utils.get_reaction_in_latex(reaction: cantera.Reaction | str, force_double_arrow: bool = False) str#

Return the LaTeX representation of a chemical reaction equation.

The output is a single matplotlib mathtext block (one pair of $) so the reaction arrow renders as a math symbol rather than literal text. Each species is formatted via get_species_in_latex(). Stoichiometric coefficients equal to 1 are omitted; non-unit coefficients are prefixed with a thin LaTeX space (\,) so digit and species don’t run together.

Parameters:
  • reaction (cantera.Reaction or str) – Either a Cantera reaction object or an equation string. A string is round-tripped through ct.Reaction (using a dummy Arrhenius rate, the same idiom used elsewhere in this codebase) so that malformed equations fail loudly through Cantera’s parser and the reversible flag is determined consistently. The literal side-of-the-arrow text is then parsed locally because Cantera’s reactants/products dictionaries report net stoichiometry and silently drop third bodies and species that appear on both sides (e.g. e- in ionization reactions), which is not what we want for display.

  • force_double_arrow (bool, optional) – If True, use a double arrow (<=>) instead of a single arrow (=>). By default False.

Returns:

Math-mode string ready to be passed to matplotlib as a label.

Return type:

str

Examples

>>> get_reaction_in_latex(
...     "CH4 + e- => CH4+ + 2 e-"
... )
'$\\mathregular{CH_{4}} + \\mathregular{e^{-}}
 \\rightharpoonup \\mathregular{CH_{4}^{+}} + 2\\,\\mathregular{e^{-}}$'
>>> get_reaction_in_latex("H + CH4 <=> CH3 + H2")
'$\\mathregular{H} + \\mathregular{CH_{4}}
 \\rightleftharpoons \\mathregular{CH_{3}} + \\mathregular{H_{2}}$'
rizer.misc.plt_utils.get_species_color(species: str) str#

Return a consistent color for a given chemical species.

Notes

  • A curated mapping is used for common species to keep colors intuitive and stable.

  • All other species fall back to a deterministic choice from the Matplotlib tab20 palette based on a stable hash of the species name.