Skip to content

Commit c43f7f9

Browse files
committed
Add and register SciFormatter
1 parent da125d4 commit c43f7f9

2 files changed

Lines changed: 54 additions & 5 deletions

File tree

proplot/constructor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
FORMATTERS = { # note default LogFormatter uses ugly e+00 notation
115115
'auto': pticker.AutoFormatter,
116116
'frac': pticker.FracFormatter,
117+
'sci': pticker.SciFormatter,
117118
'sigfig': pticker.SigFigFormatter,
118119
'simple': pticker.SimpleFormatter,
119120
'date': mdates.AutoDateFormatter,
@@ -124,9 +125,7 @@
124125
'func': mticker.FuncFormatter,
125126
'strmethod': mticker.StrMethodFormatter,
126127
'formatstr': mticker.FormatStrFormatter,
127-
'log': mticker.LogFormatterSciNotation,
128-
'sci': mticker.LogFormatterSciNotation,
129-
'math': mticker.LogFormatterMathtext,
128+
'log': mticker.LogFormatterSciNotation, # NOTE: this is subclass of Mathtext class
130129
'logit': mticker.LogitFormatter,
131130
'eng': mticker.EngFormatter,
132131
'percent': mticker.PercentFormatter,
@@ -140,6 +139,7 @@
140139
'deglon': partial(pticker.SimpleFormatter, negpos='WE', suffix='\N{DEGREE SIGN}', wraprange=(-180, 180)), # noqa: E501
141140
'dmslon': partial(pticker._LongitudeFormatter, dms=True),
142141
'dmslat': partial(pticker._LatitudeFormatter, dms=True),
142+
'math': mticker.LogFormatterMathtext, # deprecated (use SciNotation subclass)
143143
}
144144
if hasattr(mdates, 'ConciseDateFormatter'):
145145
FORMATTERS['concise'] = mdates.ConciseDateFormatter
@@ -1007,6 +1007,7 @@ def Formatter(formatter, *args, date=False, index=False, **kwargs):
10071007
====================== ============================================== ===============================================================
10081008
``'null'``, ``'none'`` `~matplotlib.ticker.NullFormatter` No tick labels
10091009
``'auto'`` `~proplot.ticker.AutoFormatter` New default tick labels for axes
1010+
``'sci'`` `~proplot.ticker.SciFormatter` Format ticks with scientific notation.
10101011
``'simple'`` `~proplot.ticker.SimpleFormatter` New default tick labels for e.g. contour labels
10111012
``'sigfig'`` `~proplot.ticker.SigFigFormatter` Format labels using the first ``N`` significant digits
10121013
``'frac'`` `~proplot.ticker.FracFormatter` Rational fractions
@@ -1018,9 +1019,8 @@ def Formatter(formatter, *args, date=False, index=False, **kwargs):
10181019
``'formatstr'`` `~matplotlib.ticker.FormatStrFormatter` From C-style ``string % format`` notation
10191020
``'func'`` `~matplotlib.ticker.FuncFormatter` Use an arbitrary function
10201021
``'index'`` `~matplotlib.ticker.IndexFormatter` List of strings corresponding to non-negative integer positions
1021-
``'log'``, ``'sci'`` `~matplotlib.ticker.LogFormatterSciNotation` For log-scale axes with scientific notation
1022+
``'log'`` `~matplotlib.ticker.LogFormatterSciNotation` For log-scale axes with scientific notation
10221023
``'logit'`` `~matplotlib.ticker.LogitFormatter` For logistic-scale axes
1023-
``'math'`` `~matplotlib.ticker.LogFormatterMathtext` For log-scale axes with math text
10241024
``'percent'`` `~matplotlib.ticker.PercentFormatter` Trailing percent sign
10251025
``'scalar'`` `~matplotlib.ticker.ScalarFormatter` Old default tick labels for axes
10261026
``'strmethod'`` `~matplotlib.ticker.StrMethodFormatter` From the ``string.format`` method

proplot/ticker.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'FracFormatter',
2323
'LongitudeLocator',
2424
'LatitudeLocator',
25+
'SciFormatter',
2526
'SigFigFormatter',
2627
'SimpleFormatter',
2728
]
@@ -384,6 +385,54 @@ def _wrap_tick_range(x, wraprange):
384385
return (x - base) % modulus + base
385386

386387

388+
def SciFormatter(precision=None, zerotrim=None):
389+
"""
390+
Return a `~matplotlib.ticker.FuncFormatter` that formats
391+
the number with scientific notation.
392+
393+
Parameters
394+
----------
395+
precision : int, optional
396+
The maximum number of digits after the decimal point. Default is ``6``
397+
when `zerotrim` is ``True`` and ``2`` otherwise.
398+
zerotrim : bool, optional
399+
Whether to trim trailing zeros. Default is
400+
"""
401+
from .config import rc
402+
zerotrim = _not_none(zerotrim, rc['formatter.zerotrim'])
403+
if precision is None:
404+
precision = 6 if zerotrim else 2
405+
406+
def func(x, pos):
407+
# Get string
408+
decimal_point = AutoFormatter._get_default_decimal_point()
409+
string = ('{:.%de}' % precision).format(x)
410+
parts = string.split('e')
411+
412+
# Trim trailing zeros
413+
significand = parts[0].rstrip(decimal_point)
414+
if zerotrim:
415+
significand = AutoFormatter._trim_trailing_zeros(significand, decimal_point)
416+
417+
# Get sign and exponent
418+
sign = parts[1][0].replace('+', '')
419+
exponent = parts[1][1:].lstrip('0')
420+
if exponent:
421+
exponent = f'10^{{{sign}{exponent}}}'
422+
if significand and exponent:
423+
string = rf'{significand}{{\times}}{exponent}'
424+
else:
425+
string = rf'{significand}{exponent}'
426+
427+
# Ensure unicode minus sign
428+
string = AutoFormatter._minus_format(string)
429+
430+
# Return TeX string
431+
return f'${string}$'
432+
433+
return mticker.FuncFormatter(func)
434+
435+
387436
def SigFigFormatter(sigfig=1, zerotrim=None):
388437
"""
389438
Return a `~matplotlib.ticker.FuncFormatter` that rounds numbers

0 commit comments

Comments
 (0)