rizer.misc.plt_utils#
Functions#
|
Set the matplotlib style for plots. |
|
Add centered text with a consistent default bbox. |
|
Add a centered annotation with an arrow and a consistent default bbox. |
|
Save the current figure to a file in the figures folder. |
|
Align zeros of the two axes, zooming them out by same ratio. |
|
Align zeros of multiple axes, zooming them out by same ratio. |
|
Return the LaTeX representation of a chemical species. |
|
Return the LaTeX representation of a chemical reaction equation. |
|
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.
- 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 (
tupleoffloat) – Coordinates of the point being annotated.xytext (
tupleoffloat) – 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 (
strorNone, optional) – Text and arrow color. If None, reuse the color of the last line in the axis.arrow_lw (
floatorNone, optional) – Line width of the arrow.arrow_linestyle (
strorNone, optional) – Line style of the arrow (e.g.,"--").arrow_relpos (
tupleoffloatorNone, optional) – Relative position of the arrow.
- 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:
fig (
matplotlib.figure.Figure) – Matplotlib figure to save.filename (
str) – Name of the file to save the figure to.
- 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:
ax1 (
matplotlib.axes.Axes) – First axis.ax2 (
matplotlib.axes.Axes) – Second axis.
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 (
listofmatplotlib.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
- 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:
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 viaget_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.Reactionorstr) – Either a Cantera reaction object or an equation string. A string is round-tripped throughct.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 thereversibleflag is determined consistently. The literal side-of-the-arrow text is then parsed locally because Cantera’sreactants/productsdictionaries 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:
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
tab20palette based on a stable hash of the species name.