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

Skip to content

Commit 9b98582

Browse files
committed
ENH ticks sublabel display is now an option
closes #7202
1 parent 31ef622 commit 9b98582

File tree

3 files changed

+63
-38
lines changed

3 files changed

+63
-38
lines changed

lib/matplotlib/scale.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,10 @@ def set_default_locators_and_formatters(self, axis):
249249
axis.set_major_locator(LogLocator(self.base))
250250
axis.set_major_formatter(LogFormatterSciNotation(self.base))
251251
axis.set_minor_locator(LogLocator(self.base, self.subs))
252-
axis.set_minor_formatter(LogFormatterSciNotation(self.base, self.subs))
252+
axis.set_minor_formatter(
253+
LogFormatterSciNotation(self.base,
254+
labelOnlyBase=self.subs,
255+
sublabel_filtering=True))
253256

254257
def get_transform(self):
255258
"""

lib/matplotlib/tests/test_scale.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ def test_log_scatter():
4646

4747
buf = io.BytesIO()
4848
fig.savefig(buf, format='svg')
49+
50+
51+
if __name__ == '__main__':
52+
import nose
53+
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/ticker.py

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -800,14 +800,27 @@ class LogFormatter(Formatter):
800800
"""
801801
Format values for log axis.
802802
"""
803-
def __init__(self, base=10.0, labelOnlyBase=True):
803+
def __init__(self, base=10.0, labelOnlyBase=False,
804+
sublabel_filtering=False):
804805
"""
805806
`base` is used to locate the decade tick, which will be the only
806807
one to be labeled if `labelOnlyBase` is ``True``.
808+
809+
Parameter
810+
---------
811+
base : float, optional, default: 10.
812+
base of the logarithm.
813+
814+
labelOnlyBase : bool, optional, default: False
815+
whether to only label decades ticks.
816+
817+
sublabel_filtering : bool, optional, default: False
818+
When set to True, label on a subset of ticks.
807819
"""
808820
self._base = base + 0.0
809821
self.labelOnlyBase = labelOnlyBase
810-
self.sublabel = [1, ]
822+
self.sublabel_filtering = sublabel_filtering
823+
self._sublabels = [1, ]
811824

812825
def base(self, base):
813826
"""
@@ -857,11 +870,11 @@ def set_locs(self, locs):
857870

858871
if numdec > 1:
859872
# Label only bases
860-
self.sublabel = set((1,))
873+
self._sublabels = set((1,))
861874
else:
862875
# Add labels between bases at log-spaced coefficients
863876
c = np.logspace(0, 1, (4 - int(numdec)) + 1, base=b)
864-
self.sublabel = set(np.round(c))
877+
self._sublabels = set(np.round(c))
865878

866879
def __call__(self, x, pos=None):
867880
"""
@@ -877,19 +890,20 @@ def __call__(self, x, pos=None):
877890
isDecade = is_close_to_int(fx)
878891
exponent = np.round(fx) if isDecade else np.floor(fx)
879892
coeff = np.round(x / b ** exponent)
880-
if coeff in self.sublabel:
881-
if not isDecade and self.labelOnlyBase:
882-
return ''
883-
elif x > 10000:
884-
s = '%1.0e' % x
885-
elif x < 1:
886-
s = '%1.0e' % x
887-
else:
888-
s = self.pprint_val(x, self.d)
889-
if sign == -1:
890-
s = '-%s' % s
893+
894+
if self.sublabel_filtering and coeff not in self._sublabels:
895+
return ''
896+
if not isDecade and self.labelOnlyBase:
897+
return ''
898+
899+
if x > 10000:
900+
s = '%1.0e' % x
901+
elif x < 1:
902+
s = '%1.0e' % x
891903
else:
892-
s = ''
904+
s = self.pprint_val(x, self.d)
905+
if sign == -1:
906+
s = '-%s' % s
893907

894908
return self.fix_minus(s)
895909

@@ -958,10 +972,17 @@ def __call__(self, x, pos=None):
958972
sign = np.sign(x)
959973
# only label the decades
960974
fx = math.log(abs(x)) / math.log(b)
975+
961976
isDecade = is_close_to_int(fx)
977+
exponent = np.round(fx) if is_decade else np.floor(fx)
978+
coeff = np.round(abs(x) / b ** exponent)
979+
980+
if self.sublabel_filtering and coeff not in self._sublabels:
981+
return ''
962982
if not isDecade and self.labelOnlyBase:
963-
s = ''
964-
elif abs(fx) > 10000:
983+
return ''
984+
985+
if abs(fx) > 10000:
965986
s = '%1.0g' % fx
966987
elif abs(fx) < 1:
967988
s = '%1.0g' % fx
@@ -1016,33 +1037,29 @@ def __call__(self, x, pos=None):
10161037
else:
10171038
base = '%s' % b
10181039

1019-
if coeff in self.sublabel:
1020-
if not is_decade and self.labelOnlyBase:
1021-
return ''
1022-
elif not is_decade:
1023-
return self._non_decade_format(sign_string, base, fx, usetex)
1024-
else:
1025-
if usetex:
1026-
return (r'$%s%s^{%d}$') % (sign_string,
1027-
base,
1028-
nearest_long(fx))
1029-
else:
1030-
return ('$%s$' % _mathdefault(
1031-
'%s%s^{%d}' %
1032-
(sign_string, base, nearest_long(fx))))
1033-
else:
1040+
if self.sublabel_filtering and coeff not in self._sublabels:
10341041
return ''
1042+
if not is_decade and self.labelOnlyBase:
1043+
return ''
1044+
1045+
if not is_decade:
1046+
return self._non_decade_format(sign_string, base, fx, usetex)
1047+
else:
1048+
if usetex:
1049+
return (r'$%s%s^{%d}$') % (sign_string,
1050+
base,
1051+
nearest_long(fx))
1052+
else:
1053+
return ('$%s$' % _mathdefault(
1054+
'%s%s^{%d}' %
1055+
(sign_string, base, nearest_long(fx))))
10351056

10361057

10371058
class LogFormatterSciNotation(LogFormatterMathtext):
10381059
"""
10391060
Format values following scientific notation in a logarithmic axis
10401061
"""
10411062

1042-
def __init__(self, base=10.0, labelOnlyBase=False):
1043-
super(LogFormatterSciNotation, self).__init__(base=base,
1044-
labelOnlyBase=labelOnlyBase)
1045-
10461063
def _non_decade_format(self, sign_string, base, fx, usetex):
10471064
'Return string for non-decade locations'
10481065
b = float(base)

0 commit comments

Comments
 (0)