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

Skip to content

Commit 584705e

Browse files
committed
logitscale: tests, rewrite of TestLogitLocator
1 parent 4891fbf commit 584705e

1 file changed

Lines changed: 104 additions & 5 deletions

File tree

lib/matplotlib/tests/test_ticker.py

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,113 @@ def assert_almost_equal(x, y):
257257

258258

259259
class TestLogitLocator:
260-
def test_set_params(self):
260+
ref_basic_limits = [
261+
(5e-2, 1 - 5e-2),
262+
(5e-3, 1 - 5e-3),
263+
(5e-4, 1 - 5e-4),
264+
(5e-5, 1 - 5e-5),
265+
(5e-6, 1 - 5e-6),
266+
(5e-7, 1 - 5e-7),
267+
(5e-8, 1 - 5e-8),
268+
(5e-9, 1 - 5e-9),
269+
]
270+
271+
ref_basic_major_ticks = [
272+
1 / (10 ** np.arange(1, 3)),
273+
1 / (10 ** np.arange(1, 4)),
274+
1 / (10 ** np.arange(1, 5)),
275+
1 / (10 ** np.arange(1, 6)),
276+
1 / (10 ** np.arange(1, 7)),
277+
1 / (10 ** np.arange(1, 8)),
278+
1 / (10 ** np.arange(1, 9)),
279+
1 / (10 ** np.arange(1, 10)),
280+
]
281+
282+
ref_maxn_limits = [(0.4, 0.6), (5e-2, 2e-1), (1 - 2e-1, 1 - 5e-2)]
283+
284+
@pytest.mark.parametrize(
285+
"lims, expected_low_ticks",
286+
zip(ref_basic_limits, ref_basic_major_ticks),
287+
)
288+
def test_basic_major(self, lims, expected_low_ticks):
261289
"""
262-
Create logit locator with default minor=False, and change it to
263-
something else. See if change was successful. Should not exception.
290+
Create logit locator with huge number of major, and tests ticks.
291+
"""
292+
expected_ticks = sorted(
293+
[*expected_low_ticks, 0.5, *(1 - expected_low_ticks)]
294+
)
295+
loc = mticker.LogitLocator(nbins=100)
296+
_LogitHelper.assert_almost_equal(
297+
loc.tick_values(*lims),
298+
expected_ticks
299+
)
300+
301+
@pytest.mark.parametrize("lims", ref_maxn_limits)
302+
def test_maxn_major(self, lims):
303+
"""
304+
When the axis is zoomed, the locator must have the same behavior as
305+
MaxNLocator.
306+
"""
307+
loc = mticker.LogitLocator(nbins=100)
308+
maxn_loc = mticker.MaxNLocator(nbins=100, steps=[1, 2, 5, 10])
309+
for nbins in (4, 8, 16):
310+
loc.set_params(nbins=nbins)
311+
maxn_loc.set_params(nbins=nbins)
312+
ticks = loc.tick_values(*lims)
313+
maxn_ticks = maxn_loc.tick_values(*lims)
314+
assert ticks.shape == maxn_ticks.shape
315+
assert (ticks == maxn_ticks).all()
316+
317+
@pytest.mark.parametrize("lims", ref_basic_limits + ref_maxn_limits)
318+
def test_nbins_major(self, lims):
264319
"""
265-
loc = mticker.LogitLocator() # Defaults to false.
266-
loc.set_params(minor=True)
320+
Assert logit locator for respecting nbins param.
321+
"""
322+
323+
basic_needed = int(-np.floor(np.log10(lims[0]))) * 2 + 1
324+
loc = mticker.LogitLocator(nbins=100)
325+
for nbins in range(basic_needed, 2, -1):
326+
loc.set_params(nbins=nbins)
327+
assert len(loc.tick_values(*lims)) <= nbins + 2
328+
329+
@pytest.mark.parametrize(
330+
"lims, expected_low_ticks",
331+
zip(ref_basic_limits, ref_basic_major_ticks),
332+
)
333+
def test_minor(self, lims, expected_low_ticks):
334+
"""
335+
In large scale, test the presence of minor,
336+
and assert no minor when major are subsampled.
337+
"""
338+
339+
expected_ticks = sorted(
340+
[*expected_low_ticks, 0.5, *(1 - expected_low_ticks)]
341+
)
342+
basic_needed = len(expected_ticks)
343+
loc = mticker.LogitLocator(nbins=100)
344+
minor_loc = mticker.LogitLocator(nbins=100, minor=True)
345+
for nbins in range(basic_needed, 2, -1):
346+
loc.set_params(nbins=nbins)
347+
minor_loc.set_params(nbins=nbins)
348+
major_ticks = loc.tick_values(*lims)
349+
minor_ticks = minor_loc.tick_values(*lims)
350+
if len(major_ticks) >= len(expected_ticks):
351+
# no subsample, we must have a lot of minors ticks
352+
assert (len(major_ticks) - 1) * 5 < len(minor_ticks)
353+
else:
354+
# subsample
355+
_LogitHelper.assert_almost_equal(
356+
np.sort(np.concatenate((major_ticks, minor_ticks))),
357+
expected_ticks,
358+
)
359+
360+
def test_minor_attr(self):
361+
loc = mticker.LogitLocator(nbins=100)
362+
assert not loc.minor
363+
loc.minor = True
267364
assert loc.minor
365+
loc.set_params(minor=False)
366+
assert not loc.minor
268367

269368

270369
class TestFixedLocator:

0 commit comments

Comments
 (0)