9
9
import locale
10
10
from fractions import Fraction
11
11
from .internals import ic # noqa: F401
12
- from .internals import _not_none
12
+ from .internals import docstring , _not_none
13
13
14
14
__all__ = [
15
15
'AutoFormatter' ,
23
23
REGEX_MINUS_ZERO = re .compile ('\\ A[-\N{MINUS SIGN} ]0(.0*)?\\ Z' )
24
24
25
25
26
+ docstring .snippets ['formatter.params' ] = """
27
+ zerotrim : bool, optional
28
+ Whether to trim trailing zeros. Default is :rc:`axes.formatter.zerotrim`.
29
+ tickrange : (float, float), optional
30
+ Range within which major tick marks are labelled. Default is ``(-np.inf, np.inf)``.
31
+ prefix, suffix : str, optional
32
+ Prefix and suffix for all strings.
33
+ negpos : str, optional
34
+ Length-2 string indicating the suffix for "negative" and "positive"
35
+ numbers, meant to replace the minus sign. This is useful for indicating
36
+ cardinal geographic coordinates.
37
+ """
38
+
39
+
26
40
class AutoFormatter (mticker .ScalarFormatter ):
27
41
"""
28
42
The new default formatter. Differs from `~matplotlib.ticker.ScalarFormatter`
@@ -33,25 +47,17 @@ class AutoFormatter(mticker.ScalarFormatter):
33
47
3. Permits adding arbitrary prefix or suffix to every tick label string.
34
48
4. Permits adding "negative" and "positive" indicator.
35
49
"""
50
+ @docstring .add_snippets
36
51
def __init__ (
37
52
self ,
38
53
zerotrim = None , tickrange = None ,
39
- prefix = None , suffix = None , negpos = None , ** kwargs
54
+ prefix = None , suffix = None , negpos = None ,
55
+ ** kwargs
40
56
):
41
57
"""
42
58
Parameters
43
59
----------
44
- zerotrim : bool, optional
45
- Whether to trim trailing zeros.
46
- Default is :rc:`axes.formatter.zerotrim`.
47
- tickrange : (float, float), optional
48
- Range within which major tick marks are labelled.
49
- prefix, suffix : str, optional
50
- Prefix and suffix for all strings.
51
- negpos : str, optional
52
- Length-2 string indicating the suffix for "negative" and "positive"
53
- numbers, meant to replace the minus sign. This is useful for
54
- indicating cardinal geographic coordinates.
60
+ %(formatter.params)s
55
61
56
62
Other parameters
57
63
----------------
@@ -219,7 +225,7 @@ def _trim_trailing_zeros(string, use_locale=None):
219
225
return string
220
226
221
227
222
- def SigFigFormatter (sigfig = 1 , zerotrim = False ):
228
+ def SigFigFormatter (sigfig = 1 , zerotrim = None ):
223
229
"""
224
230
Return a `~matplotlib.ticker.FuncFormatter` that rounds numbers
225
231
to the specified number of *significant digits*.
@@ -231,24 +237,34 @@ def SigFigFormatter(sigfig=1, zerotrim=False):
231
237
zerotrim : bool, optional
232
238
Whether to trim trailing zeros.
233
239
"""
234
- def fmt (x , pos ):
240
+ from .config import rc
241
+ zerotrim = _not_none (zerotrim , rc ['axes.formatter.zerotrim' ])
242
+
243
+ def func (x , pos ):
244
+ # Limit digits to significant figures
235
245
if x == 0 :
236
246
digits = 0
237
247
else :
238
248
digits = - int (np .log10 (abs (x )) // 1 )
239
249
digits += sigfig - 1
240
250
x = np .round (x , digits )
241
251
string = ('{:.%df}' % max (0 , digits )).format (x )
242
- if zerotrim and '.' in string :
243
- string = string .rstrip ('0' ).rstrip ('.' )
244
- string = string .replace ('-' , '\N{MINUS SIGN} ' )
245
- if string == '\N{MINUS SIGN} 0' :
246
- string = '0'
252
+ string = string .replace ('.' , AutoFormatter ._get_decimal_point ())
253
+
254
+ # Custom string formatting
255
+ string = AutoFormatter ._minus_format (string )
256
+ if zerotrim :
257
+ string = AutoFormatter ._trim_trailing_zeros (string )
247
258
return string
248
- return mticker .FuncFormatter (fmt )
249
259
260
+ return mticker .FuncFormatter (func )
250
261
251
- def SimpleFormatter (precision = 6 , zerotrim = True ):
262
+
263
+ @docstring .add_snippets
264
+ def SimpleFormatter (
265
+ precision = 6 , zerotrim = None , tickrange = None ,
266
+ prefix = None , suffix = None , negpos = None , ** kwargs
267
+ ):
252
268
"""
253
269
Return a `~matplotlib.ticker.FuncFormatter` that replicates the
254
270
`zerotrim` feature from `AutoFormatter`. This is more suitable for
@@ -258,22 +274,38 @@ def SimpleFormatter(precision=6, zerotrim=True):
258
274
Parameters
259
275
----------
260
276
precision : int, optional
261
- The maximum number of digits after the decimal point.
262
- zerotrim : bool, optional
263
- Whether to trim trailing zeros.
264
- Default is :rc:`axes.formatter.zerotrim`.
277
+ The maximum number of digits after the decimal point. Default is ``6``.
278
+ %(formatter.params)s
265
279
"""
266
280
from .config import rc
267
281
zerotrim = _not_none (zerotrim , rc ['axes.formatter.zerotrim' ])
282
+ tickrange = tickrange or (- np .inf , np .inf )
283
+ prefix = prefix or ''
284
+ suffix = suffix or ''
285
+ negpos = negpos or ''
268
286
269
287
def func (x , pos ):
288
+ # Tick range limitation
289
+ if AutoFormatter ._outside_tick_range (x , tickrange ):
290
+ return ''
291
+
292
+ # Negative positive handling
293
+ x , tail = AutoFormatter ._neg_pos_format (x , negpos )
294
+
295
+ # Default string formatting
270
296
string = ('{:.%df}' % precision ).format (x )
271
- if zerotrim and '.' in string :
272
- string = string .rstrip ('0' ).rstrip ('.' )
273
- string = string .replace ('-' , '\N{MINUS SIGN} ' )
274
- if string == '\N{MINUS SIGN} 0' :
275
- string = '0'
297
+ string = string .replace ('.' , AutoFormatter ._get_decimal_point ())
298
+
299
+ # Custom string formatting
300
+ string = AutoFormatter ._minus_format (string )
301
+ if zerotrim :
302
+ string = AutoFormatter ._trim_trailing_zeros (string )
303
+
304
+ # Prefix and suffix
305
+ string = AutoFormatter ._add_prefix_suffix (string , prefix , suffix )
306
+ string = string + tail # add negative-positive indicator
276
307
return string
308
+
277
309
return mticker .FuncFormatter (func )
278
310
279
311
@@ -309,5 +341,7 @@ def func(x, pos): # must accept location argument
309
341
string = f'-{ symbol :s} /{ frac .denominator :d} '
310
342
else : # and again make sure we use unicode minus!
311
343
string = f'{ frac .numerator :d} { symbol :s} /{ frac .denominator :d} '
312
- return string .replace ('-' , '\N{MINUS SIGN} ' )
344
+ string = AutoFormatter ._minus_format (string )
345
+ return string
346
+
313
347
return mticker .FuncFormatter (func )
0 commit comments