Skip to content

Commit 3e4a337

Browse files
authored
Merge pull request #13539 from meeseeksmachine/auto-backport-of-pr-12950-on-v3.1.x
Backport PR #12950 on branch v3.1.x (Inline or simplify FooFormatter.pprint_val.)
2 parents 13958b7 + 0673e35 commit 3e4a337

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
@@ -437,14 +437,28 @@ def __call__(self, x, pos=None):
437437
The position `pos` is ignored.
438438
"""
439439
xmin, xmax = self.axis.get_view_interval()
440+
# If the number is not too big and it's an int, format it as an int.
441+
if abs(x) < 1e4 and x == int(x):
442+
return '%d' % x
440443
d = abs(xmax - xmin)
441-
return self._pprint_val(x, d)
444+
fmt = ('%1.3e' if d < 1e-2 else
445+
'%1.3f' if d <= 1 else
446+
'%1.2f' if d <= 10 else
447+
'%1.1f' if d <= 1e5 else
448+
'%1.1e')
449+
s = fmt % x
450+
tup = s.split('e')
451+
if len(tup) == 2:
452+
mantissa = tup[0].rstrip('0').rstrip('.')
453+
sign = tup[1][0].replace('+', '')
454+
exponent = tup[1][1:].lstrip('0')
455+
s = '%se%s%s' % (mantissa, sign, exponent)
456+
else:
457+
s = s.rstrip('0').rstrip('.')
458+
return s
442459

443460
@cbook.deprecated("3.1")
444-
def pprint_val(self, *args, **kwargs):
445-
return self._pprint_val(*args, **kwargs)
446-
447-
def _pprint_val(self, x, d):
461+
def pprint_val(self, x, d):
448462
"""
449463
Formats the value `x` based on the size of the axis range `d`.
450464
"""
@@ -560,7 +574,13 @@ def __call__(self, x, pos=None):
560574
if len(self.locs) == 0:
561575
return ''
562576
else:
563-
s = self._pprint_val(x)
577+
xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
578+
if np.abs(xp) < 1e-8:
579+
xp = 0
580+
if self._useLocale:
581+
s = locale.format_string(self.format, (xp,))
582+
else:
583+
s = self.format % xp
564584
return self.fix_minus(s)
565585

566586
def set_scientific(self, b):
@@ -767,10 +787,7 @@ def _set_format(self):
767787
self.format = '$%s$' % _mathdefault(self.format)
768788

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

0 commit comments

Comments
 (0)