@@ -800,14 +800,27 @@ class LogFormatter(Formatter):
800
800
"""
801
801
Format values for log axis.
802
802
"""
803
- def __init__ (self , base = 10.0 , labelOnlyBase = True ):
803
+ def __init__ (self , base = 10.0 , labelOnlyBase = False ,
804
+ sublabel_filtering = False ):
804
805
"""
805
806
`base` is used to locate the decade tick, which will be the only
806
807
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.
807
819
"""
808
820
self ._base = base + 0.0
809
821
self .labelOnlyBase = labelOnlyBase
810
- self .sublabel = [1 , ]
822
+ self .sublabel_filtering = sublabel_filtering
823
+ self ._sublabels = [1 , ]
811
824
812
825
def base (self , base ):
813
826
"""
@@ -857,11 +870,11 @@ def set_locs(self, locs):
857
870
858
871
if numdec > 1 :
859
872
# Label only bases
860
- self .sublabel = set ((1 ,))
873
+ self ._sublabels = set ((1 ,))
861
874
else :
862
875
# Add labels between bases at log-spaced coefficients
863
876
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 ))
865
878
866
879
def __call__ (self , x , pos = None ):
867
880
"""
@@ -877,19 +890,20 @@ def __call__(self, x, pos=None):
877
890
isDecade = is_close_to_int (fx )
878
891
exponent = np .round (fx ) if isDecade else np .floor (fx )
879
892
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
891
903
else :
892
- s = ''
904
+ s = self .pprint_val (x , self .d )
905
+ if sign == - 1 :
906
+ s = '-%s' % s
893
907
894
908
return self .fix_minus (s )
895
909
@@ -958,10 +972,17 @@ def __call__(self, x, pos=None):
958
972
sign = np .sign (x )
959
973
# only label the decades
960
974
fx = math .log (abs (x )) / math .log (b )
975
+
961
976
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 ''
962
982
if not isDecade and self .labelOnlyBase :
963
- s = ''
964
- elif abs (fx ) > 10000 :
983
+ return ''
984
+
985
+ if abs (fx ) > 10000 :
965
986
s = '%1.0g' % fx
966
987
elif abs (fx ) < 1 :
967
988
s = '%1.0g' % fx
@@ -1016,33 +1037,29 @@ def __call__(self, x, pos=None):
1016
1037
else :
1017
1038
base = '%s' % b
1018
1039
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 :
1034
1041
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 ))))
1035
1056
1036
1057
1037
1058
class LogFormatterSciNotation (LogFormatterMathtext ):
1038
1059
"""
1039
1060
Format values following scientific notation in a logarithmic axis
1040
1061
"""
1041
1062
1042
- def __init__ (self , base = 10.0 , labelOnlyBase = False ):
1043
- super (LogFormatterSciNotation , self ).__init__ (base = base ,
1044
- labelOnlyBase = labelOnlyBase )
1045
-
1046
1063
def _non_decade_format (self , sign_string , base , fx , usetex ):
1047
1064
'Return string for non-decade locations'
1048
1065
b = float (base )
0 commit comments