Skip to content

Commit 8bdabcd

Browse files
committed
refactor LogFormatter classes for consistency, readability
1 parent e3de3be commit 8bdabcd

File tree

1 file changed

+28
-55
lines changed

1 file changed

+28
-55
lines changed

lib/matplotlib/ticker.py

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -902,10 +902,6 @@ def set_locs(self, locs=None):
902902
self._sublabels = None
903903
return
904904

905-
b = self._base
906-
907-
vmin, vmax = self.axis.get_view_interval()
908-
909905
# Handle symlog case:
910906
linthresh = self._linthresh
911907
if linthresh is None:
@@ -914,6 +910,7 @@ def set_locs(self, locs=None):
914910
except AttributeError:
915911
pass
916912

913+
vmin, vmax = self.axis.get_view_interval()
917914
if vmin > vmax:
918915
vmin, vmax = vmax, vmin
919916

@@ -924,6 +921,7 @@ def set_locs(self, locs=None):
924921
self._sublabels = set((1,)) # label powers of base
925922
return
926923

924+
b = self._base
927925
if linthresh is not None: # symlog
928926
# Only compute the number of decades in the logarithmic part of the
929927
# axis
@@ -953,37 +951,38 @@ def set_locs(self, locs=None):
953951
# Label all integer multiples of base**n.
954952
self._sublabels = set(np.arange(1, b + 1))
955953

954+
def _num_to_string(self, x, vmin, vmax):
955+
if x > 10000:
956+
s = '%1.0e' % x
957+
elif x < 1:
958+
s = '%1.0e' % x
959+
else:
960+
s = self.pprint_val(x, vmax - vmin)
961+
956962
def __call__(self, x, pos=None):
957963
"""
958964
Return the format for tick val `x`.
959965
"""
960-
vmin, vmax = self.axis.get_view_interval()
961-
vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander=0.05)
962-
d = abs(vmax - vmin)
963-
b = self._base
964966
if x == 0.0: # Symlog
965967
return '0'
968+
966969
sign = np.sign(x)
967970
x = abs(x)
971+
b = self._base
968972
# only label the decades
969973
fx = math.log(x) / math.log(b)
970974
is_x_decade = is_close_to_int(fx)
971975
exponent = np.round(fx) if is_x_decade else np.floor(fx)
972976
coeff = np.round(x / b ** exponent)
977+
973978
if self.labelOnlyBase and not is_x_decade:
974979
return ''
975980
if self._sublabels is not None and coeff not in self._sublabels:
976981
return ''
977982

978-
if x > 10000:
979-
s = '%1.0e' % x
980-
elif x < 1:
981-
s = '%1.0e' % x
982-
else:
983-
s = self.pprint_val(x, d)
984-
if sign == -1:
985-
s = '-%s' % s
986-
983+
vmin, vmax = self.axis.get_view_interval()
984+
vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander=0.05)
985+
s = self._num_to_string(x, vmin, vmax)
987986
return self.fix_minus(s)
988987

989988
def format_data(self, value):
@@ -1036,41 +1035,16 @@ class LogFormatterExponent(LogFormatter):
10361035
"""
10371036
Format values for log axis using ``exponent = log_base(value)``.
10381037
"""
1039-
def __call__(self, x, pos=None):
1040-
"""
1041-
Return the format for tick value `x`.
1042-
"""
1043-
vmin, vmax = self.axis.get_view_interval()
1044-
vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander=0.05)
1045-
d = abs(vmax - vmin)
1046-
b = self._base
1047-
if x == 0:
1048-
return '0'
1049-
sign = np.sign(x)
1050-
x = abs(x)
1051-
# only label the decades
1052-
fx = math.log(x) / math.log(b)
1053-
1054-
is_x_decade = is_close_to_int(fx)
1055-
exponent = np.round(fx) if is_x_decade else np.floor(fx)
1056-
coeff = np.round(x / b ** exponent)
1057-
1058-
if self.labelOnlyBase and not is_x_decade:
1059-
return ''
1060-
if self._sublabels is not None and coeff not in self._sublabels:
1061-
return ''
1062-
1038+
def _num_to_string(self, x, vmin, vmax):
1039+
fx = math.log(x) / math.log(self._base)
10631040
if abs(fx) > 10000:
10641041
s = '%1.0g' % fx
10651042
elif abs(fx) < 1:
10661043
s = '%1.0g' % fx
10671044
else:
1068-
fd = math.log(abs(d)) / math.log(b)
1045+
fd = math.log(vmax - vmin) / math.log(self._base)
10691046
s = self.pprint_val(fx, fd)
1070-
if sign == -1:
1071-
s = '-%s' % s
1072-
1073-
return self.fix_minus(s)
1047+
return s
10741048

10751049

10761050
class LogFormatterMathtext(LogFormatter):
@@ -1092,35 +1066,34 @@ def __call__(self, x, pos=None):
10921066
10931067
The position `pos` is ignored.
10941068
"""
1095-
b = self._base
10961069
usetex = rcParams['text.usetex']
1097-
1098-
# only label the decades
1099-
if x == 0:
1070+
if x == 0: # Symlog
11001071
if usetex:
11011072
return '$0$'
11021073
else:
11031074
return '$%s$' % _mathdefault('0')
11041075

11051076
sign_string = '-' if x < 0 else ''
11061077
x = abs(x)
1078+
b = self._base
11071079

1080+
# only label the decades
11081081
fx = math.log(x) / math.log(b)
11091082
is_x_decade = is_close_to_int(fx)
11101083
exponent = np.round(fx) if is_x_decade else np.floor(fx)
11111084
coeff = np.round(x / b ** exponent)
11121085

1086+
if self.labelOnlyBase and not is_x_decade:
1087+
return ''
1088+
if self._sublabels is not None and coeff not in self._sublabels:
1089+
return ''
1090+
11131091
# use string formatting of the base if it is not an integer
11141092
if b % 1 == 0.0:
11151093
base = '%d' % b
11161094
else:
11171095
base = '%s' % b
11181096

1119-
if self.labelOnlyBase and not is_x_decade:
1120-
return ''
1121-
if self._sublabels is not None and coeff not in self._sublabels:
1122-
return ''
1123-
11241097
if not is_x_decade:
11251098
return self._non_decade_format(sign_string, base, fx, usetex)
11261099
else:

0 commit comments

Comments
 (0)