Skip to content

Commit 10c05ff

Browse files
committed
Inline or simplify FooFormatter.pprint_val.
This avoids confusion between the three implementations (OldScalarFormatter, ScalarFormatter, and LogFormatter). OldScalarFormatter._pprint_val and ScalarFormatter._pprint_val are not used elsewhere and can be inlined. LogFormatter._pprint_val is also used by LogFormatterExponent and must be kept (even though it looks likely that LogFormatterExponent can also be simplified , e.g. the test `abs(math.log(x) / math.log(self._base)) > 10000` is basically never True for any realistic case -- but that'll be for another time).
1 parent d72f069 commit 10c05ff

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

lib/matplotlib/ticker.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -433,14 +433,28 @@ def __call__(self, x, pos=None):
433433
The position `pos` is ignored.
434434
"""
435435
xmin, xmax = self.axis.get_view_interval()
436+
# If the number is not too big and it's an int, format it as an int.
437+
if abs(x) < 1e4 and x == int(x):
438+
return '%d' % x
436439
d = abs(xmax - xmin)
437-
return self._pprint_val(x, d)
440+
fmt = ('%1.3e' if d < 1e-2 else
441+
'%1.3f' if d <= 1 else
442+
'%1.2f' if d <= 10 else
443+
'%1.1f' if d <= 1e5 else
444+
'%1.1e')
445+
s = fmt % x
446+
tup = s.split('e')
447+
if len(tup) == 2:
448+
mantissa = tup[0].rstrip('0').rstrip('.')
449+
sign = tup[1][0].replace('+', '')
450+
exponent = tup[1][1:].lstrip('0')
451+
s = '%se%s%s' % (mantissa, sign, exponent)
452+
else:
453+
s = s.rstrip('0').rstrip('.')
454+
return s
438455

439456
@cbook.deprecated("3.1")
440-
def pprint_val(self, *args, **kwargs):
441-
return self._pprint_val(*args, **kwargs)
442-
443-
def _pprint_val(self, x, d):
457+
def pprint_val(self, x, d):
444458
"""
445459
Formats the value `x` based on the size of the axis range `d`.
446460
"""
@@ -556,7 +570,13 @@ def __call__(self, x, pos=None):
556570
if len(self.locs) == 0:
557571
return ''
558572
else:
559-
s = self._pprint_val(x)
573+
xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
574+
if np.abs(xp) < 1e-8:
575+
xp = 0
576+
if self._useLocale:
577+
s = locale.format_string(self.format, (xp,))
578+
else:
579+
s = self.format % xp
560580
return self.fix_minus(s)
561581

562582
def set_scientific(self, b):
@@ -768,10 +788,7 @@ def _set_format(self, vmin, vmax):
768788
self.format = '$%s$' % _mathdefault(self.format)
769789

770790
@cbook.deprecated("3.1")
771-
def pprint_val(self, *args, **kwargs):
772-
return self._pprint_val(*args, **kwargs)
773-
774-
def _pprint_val(self, x):
791+
def pprint_val(self, x):
775792
xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
776793
if np.abs(xp) < 1e-8:
777794
xp = 0
@@ -1020,21 +1037,12 @@ def _pprint_val(self, x, d):
10201037
# If the number is not too big and it's an int, format it as an int.
10211038
if abs(x) < 1e4 and x == int(x):
10221039
return '%d' % x
1023-
1024-
if d < 1e-2:
1025-
fmt = '%1.3e'
1026-
elif d < 1e-1:
1027-
fmt = '%1.3f'
1028-
elif d > 1e5:
1029-
fmt = '%1.1e'
1030-
elif d > 10:
1031-
fmt = '%1.1f'
1032-
elif d > 1:
1033-
fmt = '%1.2f'
1034-
else:
1035-
fmt = '%1.3f'
1040+
fmt = ('%1.3e' if d < 1e-2 else
1041+
'%1.3f' if d <= 1 else
1042+
'%1.2f' if d <= 10 else
1043+
'%1.1f' if d <= 1e5 else
1044+
'%1.1e')
10361045
s = fmt % x
1037-
10381046
tup = s.split('e')
10391047
if len(tup) == 2:
10401048
mantissa = tup[0].rstrip('0').rstrip('.')

0 commit comments

Comments
 (0)