Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c92776

Browse files
committed
Factor out common formats strings in LogFormatter, LogFormatterExponent.
The x < 1 and x > 10000 branches can come together.
1 parent 202a277 commit 6c92776

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

lib/matplotlib/ticker.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -987,13 +987,7 @@ def set_locs(self, locs=None):
987987
self._sublabels = set(np.arange(1, b + 1))
988988

989989
def _num_to_string(self, x, vmin, vmax):
990-
if x > 10000:
991-
s = '%1.0e' % x
992-
elif x < 1:
993-
s = '%1.0e' % x
994-
else:
995-
s = self._pprint_val(x, vmax - vmin)
996-
return s
990+
return self._pprint_val(x, vmax - vmin) if 1 <= x <= 10000 else f"{x:1.0e}"
997991

998992
def __call__(self, x, pos=None):
999993
# docstring inherited
@@ -1053,15 +1047,14 @@ class LogFormatterExponent(LogFormatter):
10531047
"""
10541048
Format values for log axis using ``exponent = log_base(value)``.
10551049
"""
1050+
10561051
def _num_to_string(self, x, vmin, vmax):
10571052
fx = math.log(x) / math.log(self._base)
1058-
if abs(fx) > 10000:
1059-
s = '%1.0g' % fx
1060-
elif abs(fx) < 1:
1061-
s = '%1.0g' % fx
1062-
else:
1053+
if 1 <= abs(fx) <= 10000:
10631054
fd = math.log(vmax - vmin) / math.log(self._base)
10641055
s = self._pprint_val(fx, fd)
1056+
else:
1057+
s = f"{fx:1.0g}"
10651058
return s
10661059

10671060

0 commit comments

Comments
 (0)