From 10c05ffba148c872cbfb6c6570fc2444ca78284c Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 9 Dec 2018 00:18:36 +0100 Subject: [PATCH] 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). --- lib/matplotlib/ticker.py | 56 +++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 6cdb1294ebca..ee4622ed361e 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -433,14 +433,28 @@ def __call__(self, x, pos=None): The position `pos` is ignored. """ xmin, xmax = self.axis.get_view_interval() + # If the number is not too big and it's an int, format it as an int. + if abs(x) < 1e4 and x == int(x): + return '%d' % x d = abs(xmax - xmin) - return self._pprint_val(x, d) + fmt = ('%1.3e' if d < 1e-2 else + '%1.3f' if d <= 1 else + '%1.2f' if d <= 10 else + '%1.1f' if d <= 1e5 else + '%1.1e') + s = fmt % x + tup = s.split('e') + if len(tup) == 2: + mantissa = tup[0].rstrip('0').rstrip('.') + sign = tup[1][0].replace('+', '') + exponent = tup[1][1:].lstrip('0') + s = '%se%s%s' % (mantissa, sign, exponent) + else: + s = s.rstrip('0').rstrip('.') + return s @cbook.deprecated("3.1") - def pprint_val(self, *args, **kwargs): - return self._pprint_val(*args, **kwargs) - - def _pprint_val(self, x, d): + def pprint_val(self, x, d): """ Formats the value `x` based on the size of the axis range `d`. """ @@ -556,7 +570,13 @@ def __call__(self, x, pos=None): if len(self.locs) == 0: return '' else: - s = self._pprint_val(x) + xp = (x - self.offset) / (10. ** self.orderOfMagnitude) + if np.abs(xp) < 1e-8: + xp = 0 + if self._useLocale: + s = locale.format_string(self.format, (xp,)) + else: + s = self.format % xp return self.fix_minus(s) def set_scientific(self, b): @@ -768,10 +788,7 @@ def _set_format(self, vmin, vmax): self.format = '$%s$' % _mathdefault(self.format) @cbook.deprecated("3.1") - def pprint_val(self, *args, **kwargs): - return self._pprint_val(*args, **kwargs) - - def _pprint_val(self, x): + def pprint_val(self, x): xp = (x - self.offset) / (10. ** self.orderOfMagnitude) if np.abs(xp) < 1e-8: xp = 0 @@ -1020,21 +1037,12 @@ def _pprint_val(self, x, d): # If the number is not too big and it's an int, format it as an int. if abs(x) < 1e4 and x == int(x): return '%d' % x - - if d < 1e-2: - fmt = '%1.3e' - elif d < 1e-1: - fmt = '%1.3f' - elif d > 1e5: - fmt = '%1.1e' - elif d > 10: - fmt = '%1.1f' - elif d > 1: - fmt = '%1.2f' - else: - fmt = '%1.3f' + fmt = ('%1.3e' if d < 1e-2 else + '%1.3f' if d <= 1 else + '%1.2f' if d <= 10 else + '%1.1f' if d <= 1e5 else + '%1.1e') s = fmt % x - tup = s.split('e') if len(tup) == 2: mantissa = tup[0].rstrip('0').rstrip('.')