Skip to content

Commit 99a4cf1

Browse files
committed
logitscale: update and add tests for ticker.LogitLocator
1 parent a82119a commit 99a4cf1

File tree

1 file changed

+104
-6
lines changed

1 file changed

+104
-6
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,112 @@ def test_set_params(self):
239239

240240

241241
class TestLogitLocator:
242-
def test_set_params(self):
242+
ref_basic_limits = [
243+
(5e-2, 1 - 5e-2),
244+
(5e-3, 1 - 5e-3),
245+
(5e-4, 1 - 5e-4),
246+
(5e-5, 1 - 5e-5),
247+
(5e-6, 1 - 5e-6),
248+
(5e-7, 1 - 5e-7),
249+
(5e-8, 1 - 5e-8),
250+
(5e-9, 1 - 5e-9),
251+
(5e-10, 1 - 5e-10),
252+
]
253+
254+
ref_basic_major_ticks = [
255+
1 / (10 ** np.arange(1, 3)),
256+
1 / (10 ** np.arange(1, 4)),
257+
1 / (10 ** np.arange(1, 5)),
258+
1 / (10 ** np.arange(1, 6)),
259+
1 / (10 ** np.arange(1, 7)),
260+
1 / (10 ** np.arange(1, 8)),
261+
1 / (10 ** np.arange(1, 9)),
262+
1 / (10 ** np.arange(1, 10)),
263+
1 / (10 ** np.arange(1, 11)),
264+
]
265+
266+
ref_maxn_limits = [(0.4, 0.6), (5e-2, 2e-1), (1 - 2e-1, 1 - 5e-2)]
267+
268+
@pytest.mark.parametrize(
269+
"lims, expected_first_ticks",
270+
zip(ref_basic_limits, ref_basic_major_ticks),
271+
)
272+
def test_basic_major(self, lims, expected_first_ticks):
243273
"""
244-
Create logit locator with default minor=False, and change it to
245-
something else. See if change was successful. Should not exception.
274+
Create logit locator with huge number of major, and tests ticks.
275+
"""
276+
expected_ticks = np.sort(
277+
np.concatenate(
278+
(expected_first_ticks, (1 / 2,), 1 - expected_first_ticks)
279+
)
280+
)
281+
loc = mticker.LogitLocator(nbins=100)
282+
assert_almost_equal(loc.tick_values(*lims), expected_ticks)
283+
284+
@pytest.mark.parametrize("lims", ref_maxn_limits)
285+
def test_maxn_major(self, lims):
246286
"""
247-
loc = mticker.LogitLocator() # Defaults to false.
248-
loc.set_params(minor=True)
249-
assert loc.minor
287+
When the axis is zoomed, the locator must have the same behavior as
288+
MaxNLocator.
289+
"""
290+
loc = mticker.LogitLocator(nbins=100)
291+
maxn_loc = mticker.MaxNLocator(nbins=100, steps=[1, 2, 5, 10])
292+
for nbins in (4, 8, 16):
293+
loc.set_params(nbins=nbins)
294+
maxn_loc.set_params(nbins=nbins)
295+
ticks = loc.tick_values(*lims)
296+
maxn_ticks = maxn_loc.tick_values(*lims)
297+
assert ticks.shape == maxn_ticks.shape
298+
assert (ticks == maxn_ticks).all()
299+
300+
@pytest.mark.parametrize("lims", ref_basic_limits + ref_maxn_limits)
301+
def test_nbins_major(self, lims):
302+
"""
303+
Assert logit locator for respecting nbins param.
304+
"""
305+
306+
basic_needed = int(-np.floor(np.log10(lims[0]))) * 2 + 1
307+
loc = mticker.LogitLocator(nbins=100)
308+
for nbins in range(basic_needed, 2, -1):
309+
loc.set_params(nbins=nbins)
310+
assert len(loc.tick_values(*lims)) <= nbins + 2
311+
312+
@pytest.mark.parametrize(
313+
"lims, expected_first_ticks",
314+
zip(ref_basic_limits, ref_basic_major_ticks),
315+
)
316+
def test_minor(self, lims, expected_first_ticks):
317+
"""
318+
In large scale, test the presence of minor,
319+
and assert no minor when major are subsampled.
320+
"""
321+
322+
expected_ticks = np.sort(
323+
np.concatenate(
324+
(expected_first_ticks, (1 / 2,), 1 - expected_first_ticks)
325+
)
326+
)
327+
328+
basic_needed = len(expected_ticks)
329+
loc = mticker.LogitLocator(nbins=100)
330+
minor_loc = mticker.LogitLocator(nbins=100, minor=True)
331+
for nbins in range(basic_needed, 2, -1):
332+
loc.set_params(nbins=nbins)
333+
minor_loc.set_params(nbins=nbins)
334+
major_ticks = loc.tick_values(*lims)
335+
minor_ticks = minor_loc.tick_values(*lims)
336+
if len(major_ticks) >= len(expected_ticks):
337+
# no subsample, we must have a lot of minors ticks
338+
assert (len(major_ticks) - 1) * 5 < len(minor_ticks)
339+
else:
340+
# subsample
341+
assert len(major_ticks) + len(minor_ticks) == len(
342+
expected_ticks
343+
)
344+
assert_almost_equal(
345+
np.sort(np.concatenate((major_ticks, minor_ticks))),
346+
expected_ticks,
347+
)
250348

251349

252350
class TestFixedLocator:

0 commit comments

Comments
 (0)