Skip to content
Projects

plasma_plots.labels

Numbers in plot labels and titles, written the way a reader writes them.

format_number(0.57825) ‘0.578’ format_number(0.0) ‘0’ format_number(1.234e-5) ‘1.23×10⁻⁵’

Functions

NameDescription
format_numberWrite a number for a label: plain between 0.001 and 10000, else as a power of ten.

format_numberfunction#

def format_number(value: float, digits: int = 3, *, mathtext: bool = False) -> str

Write a number for a label: plain between 0.001 and 10000, else as a power of ten.

Parameters

NameTypeDefaultDescription
valuefloatrequiredThe number.
digitsint3Significant digits. Default: 3.
mathtextboolFalseWrite a power of ten as mathtext, "$2\times10^{19}$", which Matplotlib draws and Plotly typesets (see plasma_plots.set_math()), instead of Unicode. Default: False.

Returns

str
E.g. "0.578", "12.3", "1234", "0", "1.23×10⁻⁵"; trailing zeros are dropped.

Examples

>>> format_number(5.7825e-01)
'0.578'
>>> format_number(2.0e6)
'2×10⁶'
>>> format_number(2.0e19, mathtext=True)
'$2\times10^{19}$'