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

Skip to content

Commit 3ff8c20

Browse files
committed
add tests
1 parent 0596444 commit 3ff8c20

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ def test_AutoMinorLocator():
5050
assert_almost_equal(ax.xaxis.get_ticklocs(minor=True), test_value)
5151

5252

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+
5362
def test_LogLocator():
5463
loc = mticker.LogLocator(numticks=5)
5564
assert_raises(ValueError, loc.tick_values, 0, 1000)
@@ -63,6 +72,31 @@ def test_LogLocator():
6372
test_value = np.array([0.5, 1., 2., 4., 8., 16., 32., 64., 128., 256.])
6473
assert_almost_equal(loc.tick_values(1, 100), test_value)
6574

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+
66100

67101
def test_LinearLocator_set_params():
68102
"""
@@ -252,6 +286,44 @@ def get_view_interval(self):
252286
yield _logfe_helper, formatter, base, locs, i, expected_result
253287

254288

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+
255327
def _pprint_helper(value, domain, expected):
256328
fmt = mticker.LogFormatter()
257329
label = fmt.pprint_val(value, domain)

0 commit comments

Comments
 (0)