Repository navigation
Expand file tree
/
Copy pathtest_ticker.py
More file actions
640 lines (564 loc) · 22.8 KB
/
Copy pathtest_ticker.py
File metadata and controls
640 lines (564 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from numpy.testing import assert_almost_equal
import numpy as np
import pytest
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import warnings
class TestMaxNLocator(object):
basic_data = [
(20, 100, np.array([20., 40., 60., 80., 100.])),
(0.001, 0.0001, np.array([0., 0.0002, 0.0004, 0.0006, 0.0008, 0.001])),
(-1e15, 1e15, np.array([-1.0e+15, -5.0e+14, 0e+00, 5e+14, 1.0e+15])),
]
integer_data = [
(-0.1, 1.1, None, np.array([-1, 0, 1, 2])),
(-0.1, 0.95, None, np.array([-0.25, 0, 0.25, 0.5, 0.75, 1.0])),
(1, 55, [1, 1.5, 5, 6, 10], np.array([0, 15, 30, 45, 60])),
]
@pytest.mark.parametrize('vmin, vmax, expected', basic_data)
def test_basic(self, vmin, vmax, expected):
loc = mticker.MaxNLocator(nbins=5)
assert_almost_equal(loc.tick_values(vmin, vmax), expected)
@pytest.mark.parametrize('vmin, vmax, steps, expected', integer_data)
def test_integer(self, vmin, vmax, steps, expected):
loc = mticker.MaxNLocator(nbins=5, integer=True, steps=steps)
assert_almost_equal(loc.tick_values(vmin, vmax), expected)
class TestLinearLocator(object):
def test_basic(self):
loc = mticker.LinearLocator(numticks=3)
test_value = np.array([-0.8, -0.3, 0.2])
assert_almost_equal(loc.tick_values(-0.8, 0.2), test_value)
def test_set_params(self):
"""
Create linear locator with presets={}, numticks=2 and change it to
something else. See if change was successful. Should not exception.
"""
loc = mticker.LinearLocator(numticks=2)
loc.set_params(numticks=8, presets={(0, 1): []})
assert loc.numticks == 8
assert loc.presets == {(0, 1): []}
class TestMultipleLocator(object):
def test_basic(self):
loc = mticker.MultipleLocator(base=3.147)
test_value = np.array([-9.441, -6.294, -3.147, 0., 3.147, 6.294,
9.441, 12.588])
assert_almost_equal(loc.tick_values(-7, 10), test_value)
def test_set_params(self):
"""
Create multiple locator with 0.7 base, and change it to something else.
See if change was successful.
"""
mult = mticker.MultipleLocator(base=0.7)
mult.set_params(base=1.7)
assert mult._base == 1.7
class TestAutoMinorLocator(object):
def test_basic(self):
fig, ax = plt.subplots()
ax.set_xlim(0, 1.39)
ax.minorticks_on()
test_value = np.array([0.05, 0.1, 0.15, 0.25, 0.3, 0.35, 0.45,
0.5, 0.55, 0.65, 0.7, 0.75, 0.85, 0.9,
0.95, 1, 1.05, 1.1, 1.15, 1.25, 1.3, 1.35])
assert_almost_equal(ax.xaxis.get_ticklocs(minor=True), test_value)
class TestLogLocator(object):
def test_basic(self):
loc = mticker.LogLocator(numticks=5)
with pytest.raises(ValueError):
loc.tick_values(0, 1000)
test_value = np.array([1.00000000e-05, 1.00000000e-03, 1.00000000e-01,
1.00000000e+01, 1.00000000e+03, 1.00000000e+05,
1.00000000e+07, 1.000000000e+09])
assert_almost_equal(loc.tick_values(0.001, 1.1e5), test_value)
loc = mticker.LogLocator(base=2)
test_value = np.array([0.5, 1., 2., 4., 8., 16., 32., 64., 128., 256.])
assert_almost_equal(loc.tick_values(1, 100), test_value)
def test_set_params(self):
"""
Create log locator with default value, base=10.0, subs=[1.0],
numdecs=4, numticks=15 and change it to something else.
See if change was successful. Should not raise exception.
"""
loc = mticker.LogLocator()
loc.set_params(numticks=7, numdecs=8, subs=[2.0], base=4)
assert loc.numticks == 7
assert loc.numdecs == 8
assert loc._base == 4
assert list(loc._subs) == [2.0]
class TestNullLocator(object):
def test_set_params(self):
"""
Create null locator, and attempt to call set_params() on it.
Should not exception, and should raise a warning.
"""
loc = mticker.NullLocator()
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
loc.set_params()
assert len(w) == 1
class TestLogitLocator(object):
def test_set_params(self):
"""
Create logit locator with default minor=False, and change it to
something else. See if change was successful. Should not exception.
"""
loc = mticker.LogitLocator() # Defaults to false.
loc.set_params(minor=True)
assert loc.minor
class TestFixedLocator(object):
def test_set_params(self):
"""
Create fixed locator with 5 nbins, and change it to something else.
See if change was successful.
Should not exception.
"""
fixed = mticker.FixedLocator(range(0, 24), nbins=5)
fixed.set_params(nbins=7)
assert fixed.nbins == 7
class TestIndexLocator(object):
def test_set_params(self):
"""
Create index locator with 3 base, 4 offset. and change it to something
else. See if change was successful.
Should not exception.
"""
index = mticker.IndexLocator(base=3, offset=4)
index.set_params(base=7, offset=7)
assert index._base == 7
assert index.offset == 7
class TestSymmetricalLogLocator(object):
def test_set_params(self):
"""
Create symmetrical log locator with default subs =[1.0] numticks = 15,
and change it to something else.
See if change was successful.
Should not exception.
"""
sym = mticker.SymmetricalLogLocator(base=10, linthresh=1)
sym.set_params(subs=[2.0], numticks=8)
assert sym._subs == [2.0]
assert sym.numticks == 8
class TestScalarFormatter(object):
offset_data = [
(123, 189, 0),
(-189, -123, 0),
(12341, 12349, 12340),
(-12349, -12341, -12340),
(99999.5, 100010.5, 100000),
(-100010.5, -99999.5, -100000),
(99990.5, 100000.5, 100000),
(-100000.5, -99990.5, -100000),
(1233999, 1234001, 1234000),
(-1234001, -1233999, -1234000),
(1, 1, 1),
(123, 123, 120),
# Test cases courtesy of @WeatherGod
(.4538, .4578, .45),
(3789.12, 3783.1, 3780),
(45124.3, 45831.75, 45000),
(0.000721, 0.0007243, 0.00072),
(12592.82, 12591.43, 12590),
(9., 12., 0),
(900., 1200., 0),
(1900., 1200., 0),
(0.99, 1.01, 1),
(9.99, 10.01, 10),
(99.99, 100.01, 100),
(5.99, 6.01, 6),
(15.99, 16.01, 16),
(-0.452, 0.492, 0),
(-0.492, 0.492, 0),
(12331.4, 12350.5, 12300),
(-12335.3, 12335.3, 0),
]
use_offset_data = [True, False]
@pytest.mark.parametrize('left, right, offset', offset_data)
def test_offset_value(self, left, right, offset):
fig, ax = plt.subplots()
formatter = ax.get_xaxis().get_major_formatter()
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', 'Attempting to set identical',
UserWarning)
ax.set_xlim(left, right)
assert len(w) == (1 if left == right else 0)
# Update ticks.
next(ax.get_xaxis().iter_ticks())
assert formatter.offset == offset
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', 'Attempting to set identical',
UserWarning)
ax.set_xlim(right, left)
assert len(w) == (1 if left == right else 0)
# Update ticks.
next(ax.get_xaxis().iter_ticks())
assert formatter.offset == offset
@pytest.mark.parametrize('use_offset', use_offset_data)
def test_use_offset(self, use_offset):
with matplotlib.rc_context({'axes.formatter.useoffset': use_offset}):
tmp_form = mticker.ScalarFormatter()
assert use_offset == tmp_form.get_useOffset()
class FakeAxis(object):
"""Allow Formatter to be called without having a "full" plot set up."""
def __init__(self, vmin=1, vmax=10):
self.vmin = vmin
self.vmax = vmax
def get_view_interval(self):
return self.vmin, self.vmax
class TestLogFormatterExponent(object):
param_data = [
(True, 4, np.arange(-3, 4.0), np.arange(-3, 4.0),
['-3', '-2', '-1', '0', '1', '2', '3']),
# With labelOnlyBase=False, non-integer powers should be nicely
# formatted.
(False, 10, np.array([0.1, 0.00001, np.pi, 0.2, -0.2, -0.00001]),
range(6), ['0.1', '1e-05', '3.14', '0.2', '-0.2', '-1e-05']),
(False, 50, np.array([3, 5, 12, 42], dtype='float'), range(6),
['3', '5', '12', '42']),
]
base_data = [2.0, 5.0, 10.0, np.pi, np.e]
@pytest.mark.parametrize(
'labelOnlyBase, exponent, locs, positions, expected', param_data)
@pytest.mark.parametrize('base', base_data)
def test_basic(self, labelOnlyBase, base, exponent, locs, positions,
expected):
formatter = mticker.LogFormatterExponent(base=base,
labelOnlyBase=labelOnlyBase)
formatter.axis = FakeAxis(1, base**exponent)
vals = base**locs
labels = [formatter(x, pos) for (x, pos) in zip(vals, positions)]
assert labels == expected
def test_blank(self):
# Should be a blank string for non-integer powers if labelOnlyBase=True
formatter = mticker.LogFormatterExponent(base=10, labelOnlyBase=True)
formatter.axis = FakeAxis()
assert formatter(10**0.1) == ''
class TestLogFormatterMathtext():
fmt = mticker.LogFormatterMathtext()
test_data = [
(0, 1, '$\\mathdefault{10^{0}}$'),
(0, 1e-2, '$\\mathdefault{10^{-2}}$'),
(0, 1e2, '$\\mathdefault{10^{2}}$'),
(3, 1, '$\\mathdefault{1}$'),
(3, 1e-2, '$\\mathdefault{0.01}$'),
(3, 1e2, '$\\mathdefault{100}$'),
(3, 1e-3, '$\\mathdefault{10^{-3}}$'),
(3, 1e3, '$\\mathdefault{10^{3}}$'),
]
@pytest.mark.parametrize('min_exponent, value, expected', test_data)
def test_min_exponent(self, min_exponent, value, expected):
with matplotlib.rc_context({'axes.formatter.min_exponent':
min_exponent}):
assert self.fmt(value) == expected
class TestLogFormatterSciNotation(object):
test_data = [
(2, 0.03125, '$\\mathdefault{2^{-5}}$'),
(2, 1, '$\\mathdefault{2^{0}}$'),
(2, 32, '$\\mathdefault{2^{5}}$'),
(2, 0.0375, '$\\mathdefault{1.2\\times2^{-5}}$'),
(2, 1.2, '$\\mathdefault{1.2\\times2^{0}}$'),
(2, 38.4, '$\\mathdefault{1.2\\times2^{5}}$'),
(10, -1, '$\\mathdefault{-10^{0}}$'),
(10, 1e-05, '$\\mathdefault{10^{-5}}$'),
(10, 1, '$\\mathdefault{10^{0}}$'),
(10, 100000, '$\\mathdefault{10^{5}}$'),
(10, 2e-05, '$\\mathdefault{2\\times10^{-5}}$'),
(10, 2, '$\\mathdefault{2\\times10^{0}}$'),
(10, 200000, '$\\mathdefault{2\\times10^{5}}$'),
(10, 5e-05, '$\\mathdefault{5\\times10^{-5}}$'),
(10, 5, '$\\mathdefault{5\\times10^{0}}$'),
(10, 500000, '$\\mathdefault{5\\times10^{5}}$'),
]
@pytest.mark.style('default')
@pytest.mark.parametrize('base, value, expected', test_data)
def test_basic(self, base, value, expected):
formatter = mticker.LogFormatterSciNotation(base=base)
formatter.sublabel = {1, 2, 5, 1.2}
with matplotlib.rc_context({'text.usetex': False}):
assert formatter(value) == expected
class TestLogFormatter(object):
pprint_data = [
(3.141592654e-05, 0.001, '3.142e-5'),
(0.0003141592654, 0.001, '3.142e-4'),
(0.003141592654, 0.001, '3.142e-3'),
(0.03141592654, 0.001, '3.142e-2'),
(0.3141592654, 0.001, '3.142e-1'),
(3.141592654, 0.001, '3.142'),
(31.41592654, 0.001, '3.142e1'),
(314.1592654, 0.001, '3.142e2'),
(3141.592654, 0.001, '3.142e3'),
(31415.92654, 0.001, '3.142e4'),
(314159.2654, 0.001, '3.142e5'),
(1e-05, 0.001, '1e-5'),
(0.0001, 0.001, '1e-4'),
(0.001, 0.001, '1e-3'),
(0.01, 0.001, '1e-2'),
(0.1, 0.001, '1e-1'),
(1, 0.001, '1'),
(10, 0.001, '10'),
(100, 0.001, '100'),
(1000, 0.001, '1000'),
(10000, 0.001, '1e4'),
(100000, 0.001, '1e5'),
(3.141592654e-05, 0.015, '0'),
(0.0003141592654, 0.015, '0'),
(0.003141592654, 0.015, '0.003'),
(0.03141592654, 0.015, '0.031'),
(0.3141592654, 0.015, '0.314'),
(3.141592654, 0.015, '3.142'),
(31.41592654, 0.015, '31.416'),
(314.1592654, 0.015, '314.159'),
(3141.592654, 0.015, '3141.593'),
(31415.92654, 0.015, '31415.927'),
(314159.2654, 0.015, '314159.265'),
(1e-05, 0.015, '0'),
(0.0001, 0.015, '0'),
(0.001, 0.015, '0.001'),
(0.01, 0.015, '0.01'),
(0.1, 0.015, '0.1'),
(1, 0.015, '1'),
(10, 0.015, '10'),
(100, 0.015, '100'),
(1000, 0.015, '1000'),
(10000, 0.015, '10000'),
(100000, 0.015, '100000'),
(3.141592654e-05, 0.5, '0'),
(0.0003141592654, 0.5, '0'),
(0.003141592654, 0.5, '0.003'),
(0.03141592654, 0.5, '0.031'),
(0.3141592654, 0.5, '0.314'),
(3.141592654, 0.5, '3.142'),
(31.41592654, 0.5, '31.416'),
(314.1592654, 0.5, '314.159'),
(3141.592654, 0.5, '3141.593'),
(31415.92654, 0.5, '31415.927'),
(314159.2654, 0.5, '314159.265'),
(1e-05, 0.5, '0'),
(0.0001, 0.5, '0'),
(0.001, 0.5, '0.001'),
(0.01, 0.5, '0.01'),
(0.1, 0.5, '0.1'),
(1, 0.5, '1'),
(10, 0.5, '10'),
(100, 0.5, '100'),
(1000, 0.5, '1000'),
(10000, 0.5, '10000'),
(100000, 0.5, '100000'),
(3.141592654e-05, 5, '0'),
(0.0003141592654, 5, '0'),
(0.003141592654, 5, '0'),
(0.03141592654, 5, '0.03'),
(0.3141592654, 5, '0.31'),
(3.141592654, 5, '3.14'),
(31.41592654, 5, '31.42'),
(314.1592654, 5, '314.16'),
(3141.592654, 5, '3141.59'),
(31415.92654, 5, '31415.93'),
(314159.2654, 5, '314159.27'),
(1e-05, 5, '0'),
(0.0001, 5, '0'),
(0.001, 5, '0'),
(0.01, 5, '0.01'),
(0.1, 5, '0.1'),
(1, 5, '1'),
(10, 5, '10'),
(100, 5, '100'),
(1000, 5, '1000'),
(10000, 5, '10000'),
(100000, 5, '100000'),
(3.141592654e-05, 100, '0'),
(0.0003141592654, 100, '0'),
(0.003141592654, 100, '0'),
(0.03141592654, 100, '0'),
(0.3141592654, 100, '0.3'),
(3.141592654, 100, '3.1'),
(31.41592654, 100, '31.4'),
(314.1592654, 100, '314.2'),
(3141.592654, 100, '3141.6'),
(31415.92654, 100, '31415.9'),
(314159.2654, 100, '314159.3'),
(1e-05, 100, '0'),
(0.0001, 100, '0'),
(0.001, 100, '0'),
(0.01, 100, '0'),
(0.1, 100, '0.1'),
(1, 100, '1'),
(10, 100, '10'),
(100, 100, '100'),
(1000, 100, '1000'),
(10000, 100, '10000'),
(100000, 100, '100000'),
(3.141592654e-05, 1000000.0, '3.1e-5'),
(0.0003141592654, 1000000.0, '3.1e-4'),
(0.003141592654, 1000000.0, '3.1e-3'),
(0.03141592654, 1000000.0, '3.1e-2'),
(0.3141592654, 1000000.0, '3.1e-1'),
(3.141592654, 1000000.0, '3.1'),
(31.41592654, 1000000.0, '3.1e1'),
(314.1592654, 1000000.0, '3.1e2'),
(3141.592654, 1000000.0, '3.1e3'),
(31415.92654, 1000000.0, '3.1e4'),
(314159.2654, 1000000.0, '3.1e5'),
(1e-05, 1000000.0, '1e-5'),
(0.0001, 1000000.0, '1e-4'),
(0.001, 1000000.0, '1e-3'),
(0.01, 1000000.0, '1e-2'),
(0.1, 1000000.0, '1e-1'),
(1, 1000000.0, '1'),
(10, 1000000.0, '10'),
(100, 1000000.0, '100'),
(1000, 1000000.0, '1000'),
(10000, 1000000.0, '1e4'),
(100000, 1000000.0, '1e5'),
]
@pytest.mark.parametrize('value, domain, expected', pprint_data)
def test_pprint(self, value, domain, expected):
fmt = mticker.LogFormatter()
label = fmt.pprint_val(value, domain)
assert label == expected
def _sub_labels(self, axis, subs=()):
"Test whether locator marks subs to be labeled"
fmt = axis.get_minor_formatter()
minor_tlocs = axis.get_minorticklocs()
fmt.set_locs(minor_tlocs)
coefs = minor_tlocs / 10**(np.floor(np.log10(minor_tlocs)))
label_expected = [np.round(c) in subs for c in coefs]
label_test = [fmt(x) != '' for x in minor_tlocs]
assert label_test == label_expected
@pytest.mark.style('default')
def test_sublabel(self):
# test label locator
fig, ax = plt.subplots()
ax.set_xscale('log')
ax.xaxis.set_major_locator(mticker.LogLocator(base=10, subs=[]))
ax.xaxis.set_minor_locator(mticker.LogLocator(base=10,
subs=np.arange(2, 10)))
ax.xaxis.set_major_formatter(mticker.LogFormatter(labelOnlyBase=True))
ax.xaxis.set_minor_formatter(mticker.LogFormatter(labelOnlyBase=False))
# axis range above 3 decades, only bases are labeled
ax.set_xlim(1, 1e4)
fmt = ax.xaxis.get_major_formatter()
fmt.set_locs(ax.xaxis.get_majorticklocs())
show_major_labels = [fmt(x) != ''
for x in ax.xaxis.get_majorticklocs()]
assert np.all(show_major_labels)
self._sub_labels(ax.xaxis, subs=[])
# For the next two, if the numdec threshold in LogFormatter.set_locs
# were 3, then the label sub would be 3 for 2-3 decades and (2,5)
# for 1-2 decades. With a threshold of 1, subs are not labeled.
# axis range at 2 to 3 decades
ax.set_xlim(1, 800)
self._sub_labels(ax.xaxis, subs=[])
# axis range at 1 to 2 decades
ax.set_xlim(1, 80)
self._sub_labels(ax.xaxis, subs=[])
# axis range at 0.4 to 1 decades, label subs 2, 3, 4, 6
ax.set_xlim(1, 8)
self._sub_labels(ax.xaxis, subs=[2, 3, 4, 6])
# axis range at 0 to 0.4 decades, label all
ax.set_xlim(0.5, 0.9)
self._sub_labels(ax.xaxis, subs=np.arange(2, 10, dtype=int))
@pytest.mark.parametrize('val', [1, 10, 100, 1000])
def test_LogFormatter_call(self, val):
# test _num_to_string method used in __call__
temp_lf = mticker.LogFormatter()
temp_lf.axis = FakeAxis()
assert temp_lf(val) == str(val)
class TestFormatStrFormatter(object):
def test_basic(self):
# test % style formatter
tmp_form = mticker.FormatStrFormatter('%05d')
assert '00002' == tmp_form(2)
class TestStrMethodFormatter(object):
test_data = [
('{x:05d}', (2,), '00002'),
('{x:03d}-{pos:02d}', (2, 1), '002-01'),
]
@pytest.mark.parametrize('format, input, expected', test_data)
def test_basic(self, format, input, expected):
fmt = mticker.StrMethodFormatter(format)
assert fmt(*input) == expected
class TestEngFormatter(object):
format_data = [
('', 0.1, u'100 m'),
('', 1, u'1'),
('', 999.9, u'999.9'),
('', 1001, u'1.001 k'),
(u's', 0.1, u'100 ms'),
(u's', 1, u'1 s'),
(u's', 999.9, u'999.9 s'),
(u's', 1001, u'1.001 ks'),
]
@pytest.mark.parametrize('unit, input, expected', format_data)
def test_formatting(self, unit, input, expected):
"""
Test the formatting of EngFormatter with some inputs, against
instances with and without units. Cases focus on when no SI
prefix is present, for values in [1, 1000).
"""
fmt = mticker.EngFormatter(unit)
assert fmt(input) == expected
class TestPercentFormatter(object):
percent_data = [
# Check explicitly set decimals over different intervals and values
(100, 0, '%', 120, 100, '120%'),
(100, 0, '%', 100, 90, '100%'),
(100, 0, '%', 90, 50, '90%'),
(100, 0, '%', -1.7, 40, '-2%'),
(100, 1, '%', 90.0, 100, '90.0%'),
(100, 1, '%', 80.1, 90, '80.1%'),
(100, 1, '%', 70.23, 50, '70.2%'),
# 60.554 instead of 60.55: see https://bugs.python.org/issue5118
(100, 1, '%', -60.554, 40, '-60.6%'),
# Check auto decimals over different intervals and values
(100, None, '%', 95, 1, '95.00%'),
(1.0, None, '%', 3, 6, '300%'),
(17.0, None, '%', 1, 8.5, '6%'),
(17.0, None, '%', 1, 8.4, '5.9%'),
(5, None, '%', -100, 0.000001, '-2000.00000%'),
# Check percent symbol
(1.0, 2, None, 1.2, 100, '120.00'),
(75, 3, '', 50, 100, '66.667'),
(42, None, '^^Foobar$$', 21, 12, '50.0^^Foobar$$'),
]
percent_ids = [
# Check explicitly set decimals over different intervals and values
'decimals=0, x>100%',
'decimals=0, x=100%',
'decimals=0, x<100%',
'decimals=0, x<0%',
'decimals=1, x>100%',
'decimals=1, x=100%',
'decimals=1, x<100%',
'decimals=1, x<0%',
# Check auto decimals over different intervals and values
'autodecimal, x<100%, display_range=1',
'autodecimal, x>100%, display_range=6 (custom xmax test)',
'autodecimal, x<100%, display_range=8.5 (autodecimal test 1)',
'autodecimal, x<100%, display_range=8.4 (autodecimal test 2)',
'autodecimal, x<-100%, display_range=1e-6 (tiny display range)',
# Check percent symbol
'None as percent symbol',
'Empty percent symbol',
'Custom percent symbol',
]
latex_data = [
(False, False, r'50\{t}%'),
(False, True, r'50\\\{t\}\%'),
(True, False, r'50\{t}%'),
(True, True, r'50\{t}%'),
]
@pytest.mark.parametrize(
'xmax, decimals, symbol, x, display_range, expected',
percent_data, ids=percent_ids)
def test_basic(self, xmax, decimals, symbol,
x, display_range, expected):
formatter = mticker.PercentFormatter(xmax, decimals, symbol)
with matplotlib.rc_context(rc={'text.usetex': False}):
assert formatter.format_pct(x, display_range) == expected
@pytest.mark.parametrize('is_latex, usetex, expected', latex_data)
def test_latex(self, is_latex, usetex, expected):
fmt = mticker.PercentFormatter(symbol='\\{t}%', is_latex=is_latex)
with matplotlib.rc_context(rc={'text.usetex': usetex}):
assert fmt.format_pct(50, 100) == expected