@@ -50,6 +50,15 @@ def test_AutoMinorLocator():
50
50
assert_almost_equal (ax .xaxis .get_ticklocs (minor = True ), test_value )
51
51
52
52
53
+ def _sub_labels (axis , subs = ()):
54
+ "Test whether locator marks subs to be labeled"
55
+ loc = axis .get_minor_locator ()
56
+ minor_tlocs = axis .get_minorticklocs ()
57
+ coefs = minor_tlocs / 10 ** (np .floor (np .log10 (minor_tlocs )))
58
+ label = [np .round (c ) in subs for c in coefs ]
59
+ assert_equal (loc .show_tick_label (minor_tlocs ), label )
60
+
61
+
53
62
def test_LogLocator ():
54
63
loc = mticker .LogLocator (numticks = 5 )
55
64
assert_raises (ValueError , loc .tick_values , 0 , 1000 )
@@ -63,6 +72,31 @@ def test_LogLocator():
63
72
test_value = np .array ([0.5 , 1. , 2. , 4. , 8. , 16. , 32. , 64. , 128. , 256. ])
64
73
assert_almost_equal (loc .tick_values (1 , 100 ), test_value )
65
74
75
+ # test label locator
76
+ fig , ax = plt .subplots ()
77
+ ax .set_xscale ('log' )
78
+ ax .xaxis .set_major_locator (mticker .LogLocator (base = 10 , subs = []))
79
+ ax .xaxis .set_minor_locator (mticker .LogLocator (base = 10 ,
80
+ subs = np .arange (2 , 10 )))
81
+ # axis range above 3 decades, only bases are labeled
82
+ ax .set_xlim (1 , 1e4 )
83
+ loc = ax .xaxis .get_major_locator ()
84
+ show_major_labels = loc .show_tick_label (ax .xaxis .get_majorticklocs ())
85
+ assert np .all (show_major_labels )
86
+ _sub_labels (ax .xaxis , subs = [])
87
+
88
+ # axis range at 2 to 3 decades, label sub 3
89
+ ax .set_xlim (1 , 800 )
90
+ _sub_labels (ax .xaxis , subs = [3 ])
91
+
92
+ # axis range at 1 to 2 decades, label subs 2 and 5
93
+ ax .set_xlim (1 , 80 )
94
+ _sub_labels (ax .xaxis , subs = [2 , 5 ])
95
+
96
+ # axis range at 0 to 1 decades, label subs 2, 3, 6
97
+ ax .set_xlim (1 , 8 )
98
+ _sub_labels (ax .xaxis , subs = [2 , 3 , 6 ])
99
+
66
100
67
101
def test_LinearLocator_set_params ():
68
102
"""
@@ -252,6 +286,44 @@ def get_view_interval(self):
252
286
yield _logfe_helper , formatter , base , locs , i , expected_result
253
287
254
288
289
+ def test_LogFormatterSciNotation ():
290
+ test_cases = {
291
+ 10 : (
292
+ (1e-05 , '${10^{-5}}$' ),
293
+ (1 , '${10^{0}}$' ),
294
+ (100000 , '${10^{5}}$' ),
295
+ (2e-05 , '${2\\ times10^{-5}}$' ),
296
+ (2 , '${2\\ times10^{0}}$' ),
297
+ (200000 , '${2\\ times10^{5}}$' ),
298
+ (3.1415926535897935e-05 , '${3.14159\\ times10^{-5}}$' ),
299
+ (3.141592653589793 , '${3.14159\\ times10^{0}}$' ),
300
+ (314159.2653589793 , '${3.14159\\ times10^{5}}$' ),
301
+ (5e-05 , '${5\\ times10^{-5}}$' ),
302
+ (5 , '${5\\ times10^{0}}$' ),
303
+ (500000 , '${5\\ times10^{5}}$' ),
304
+ (8.5e-05 , '${8.5\\ times10^{-5}}$' ),
305
+ (8.5 , '${8.5\\ times10^{0}}$' ),
306
+ (850000.0 , '${8.5\\ times10^{5}}$' ),
307
+ ),
308
+ 2 : (
309
+ (0.03125 , '${2^{-5}}$' ),
310
+ (1 , '${2^{0}}$' ),
311
+ (32 , '${2^{5}}$' ),
312
+ (0.0375 , '${1.2\\ times2^{-5}}$' ),
313
+ (1.2 , '${1.2\\ times2^{0}}$' ),
314
+ (38.4 , '${1.2\\ times2^{5}}$' ),
315
+ (0.044194173824159223 , '${1.41421\\ times2^{-5}}$' ),
316
+ (1.4142135623730951 , '${1.41421\\ times2^{0}}$' ),
317
+ (45.254833995939045 , '${1.41421\\ times2^{5}}$' )),
318
+ }
319
+
320
+ for base in test_cases .keys ():
321
+ formatter = mticker .LogFormatterSciNotation (base = base )
322
+ for value , expected in test_cases [base ]:
323
+ with matplotlib .rc_context ({'text.usetex' : False }):
324
+ nose .tools .assert_equal (formatter (value ), expected )
325
+
326
+
255
327
def _pprint_helper (value , domain , expected ):
256
328
fmt = mticker .LogFormatter ()
257
329
label = fmt .pprint_val (value , domain )
0 commit comments