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

Skip to content

Commit 4bbdf86

Browse files
committed
remove redundancy
LogFormatterSciNotation is now a subclass of LogFormatterMathtext, and the differences (how labels are formatted for non-decade ticks) have been factored out in a `_non_decade_format` function.
1 parent de02706 commit 4bbdf86

File tree

1 file changed

+23
-73
lines changed

1 file changed

+23
-73
lines changed

lib/matplotlib/ticker.py

Lines changed: 23 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,6 @@ def pprint_val(self, x, d):
738738
s = s.rstrip('0').rstrip('.')
739739
return s
740740

741-
SPECIAL_LOG_EXP = set((-1, 0, 1,))
742-
743741
class LogFormatterExponent(LogFormatter):
744742
"""
745743
Format values for log axis; using ``exponent = log_base(value)``
@@ -777,6 +775,13 @@ class LogFormatterMathtext(LogFormatter):
777775
Format values for log axis; using ``exponent = log_base(value)``
778776
"""
779777

778+
def _non_decade_format(self, sign_string, base, fx, usetex):
779+
'Return string for non-decade locations'
780+
if usetex:
781+
return (r'$%s%s^{%.2f}$') % (sign_string, base, fx)
782+
else:
783+
return ('$\mathdefault{%s%s^{%.2f}}$') % (sign_string, base, fx)
784+
780785
def __call__(self, x, pos=None):
781786
'Return the format for tick val *x* at position *pos*'
782787
b = self._base
@@ -803,12 +808,7 @@ def __call__(self, x, pos=None):
803808
if not is_decade and self.labelOnlyBase:
804809
return ''
805810
elif not is_decade:
806-
if usetex:
807-
return (r'$%s%s^{%.2f}$') % \
808-
(sign_string, base, fx)
809-
else:
810-
return ('$\mathdefault{%s%s^{%.2f}}$') % \
811-
(sign_string, base, fx)
811+
return self._non_decade_format(sign_string, base, fx, usetex)
812812
else:
813813
if usetex:
814814
return (r'$%s%s^{%d}$') % (sign_string,
@@ -819,78 +819,28 @@ def __call__(self, x, pos=None):
819819
base,
820820
nearest_long(fx))
821821

822-
class LogFormatterSciNotation(LogFormatter):
822+
class LogFormatterSciNotation(LogFormatterMathtext):
823823
"""
824824
Format values following scientific notation in a logarithmic axis
825-
826-
Can be used for both minor and major ticks, will follow the same logic as
827-
LogLocator with autosub to show minor labels as needed
828825
"""
829826

830-
def __call__(self, x, pos=None):
831-
'Return the format for tick val *x* at position *pos*'
832-
b = self._base
833-
usetex = rcParams['text.usetex']
834-
835-
if x == 0:
836-
if usetex:
837-
return '$0$'
838-
else:
839-
return '$\mathdefault{0}$'
840-
841-
fx = math.log(abs(x)) / math.log(b)
842-
is_decade = is_close_to_int(fx)
843-
if is_decade:
844-
exponent = nearest_long(fx)
845-
coeff = 1
846-
else:
847-
exponent = math.floor(fx)
848-
coeff = x / b ** exponent
849-
850-
sign_string = '-' if x < 0 else ''
851-
852-
# Do not show the base if x is between 0.1 and 10
853-
if exponent in SPECIAL_LOG_EXP:
854-
if is_close_to_int(x):
855-
if usetex:
856-
return '$%d$' % x
857-
else:
858-
return r'$\mathdefault{%d}$' % x
859-
else:
860-
if usetex:
861-
return '$%s$' % x
862-
else:
863-
return r'$\mathdefault{%s}$' % x
827+
def __init__(self, base=10.0, labelOnlyBase=False):
828+
super(LogFormatterSciNotation, self).__init__(base=base,
829+
labelOnlyBase=labelOnlyBase)
864830

865-
# use string formatting of the base if it is not an integer
866-
if is_close_to_int(b):
867-
base = '%d' % b
868-
else:
869-
base = '%s' % b
870-
871-
# use string formatting of the coefficient if it is not an integer
831+
def _non_decade_format(self, sign_string, base, fx, usetex):
832+
'Return string for non-decade locations'
833+
b = float(base)
834+
exponent = math.floor(fx)
835+
coeff = b ** fx / b ** exponent
872836
if is_close_to_int(coeff):
873-
coeff = '%d' % coeff
874-
else:
875-
coeff = '%s' % coeff
876-
877-
if is_decade:
878-
if usetex:
879-
return (r'$%s%s^{%d}$') % (sign_string,
880-
base,
881-
nearest_long(fx))
882-
else:
883-
return (r'$\mathdefault{%s%s^{%d}}$') % (sign_string,
884-
base,
885-
nearest_long(fx))
837+
coeff = nearest_long(coeff)
838+
if usetex:
839+
return (r'$%g\times%s^{%d}$') % \
840+
(coeff, base, exponent)
886841
else:
887-
if usetex:
888-
return (r'$%s\times%s^{%d}$') % \
889-
(coeff, base, exponent)
890-
else:
891-
return (r'$\mathdefault{%s\times%s^{%d}}$') % \
892-
(coeff, base, exponent)
893-
842+
return (r'$\mathdefault{%g\times%s^{%d}}$') % \
843+
(coeff, base, exponent)
894844

895845
class LogitFormatter(Formatter):
896846
'''Probability formatter (using Math text)'''

0 commit comments

Comments
 (0)