Skip to content

Commit cc485ae

Browse files
committed
BUG: Fixed bugs and addressed review comments
Made class attributes private Using 4 inputs instead of two of variable type Fixed docs and __all__ to correspond to actual PR Fixed numpydoc formatting
1 parent 836a34d commit cc485ae

File tree

2 files changed

+61
-57
lines changed

2 files changed

+61
-57
lines changed

lib/matplotlib/tests/test_ticker.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -539,39 +539,44 @@ def test_percentformatter(xmax, decimals, symbol, x, display_range, expected):
539539
('{x:b}', 8, False, -2, '-10'),
540540
('{x:b}', 8, True, -2, '11111110'),
541541
('{x:b}', 8, True, -700, '1000100'),
542+
('{x:b}', None, True, -500, '1000001100'),
542543
# Octal
543544
('{x:o}', 8, False, 2, '2'),
544545
('{x:o}', 8, True, 2, '2'),
545546
('{x:o}', 8, False, -2, '-2'),
546547
('{x:o}', 8, True, -2, '376'),
547548
('{x:o}', 8, True, -700, '104'),
549+
('{x:o}', None, True, -500, '1014'),
548550
# Decimal
549551
('{x:d}', 8, False, 2, '2'),
550552
('{x:d}', 8, True, 2, '2'),
551553
('{x:d}', 8, False, -2, '-2'),
552554
('{x:d}', 8, True, -2, '254'),
553555
('{x:d}', 8, True, -700, '68'),
556+
('{x:d}', None, True, -500, '524'),
554557
# n format omitted to avoid localization issues
555558
# Hexadecimal (lower)
556559
('{x:x}', 8, False, 0xC, 'c'),
557560
('{x:x}', 8, True, 0xC, 'c'),
558561
('{x:x}', 8, False, -0xC, '-c'),
559562
('{x:x}', 8, True, -0xC, 'f4'),
560563
('{x:04x}', 8, True, -630, '008a'),
564+
('{x:x}', None, True, -500, '20c'),
561565
# Hexadecimal (upper)
562566
('{x:X}', 8, False, 0xC, 'C'),
563567
('{x:X}', 8, True, 0xC, 'C'),
564568
('{x:X}', 8, False, -0xC, '-C'),
565569
('{x:X}', 8, True, -0xC, 'F4'),
566570
('{x:04X}', 8, True, -630, '008A'),
571+
('{x:X}', None, True, -500, '20C'),
567572
])
568573
def test_IntegerFormatter(fmt, bits, unsigned, value, expected):
569574
"""
570575
Verifies that numbers get converted to different bases correctly,
571576
and that floating point numbers get truncated.
572577
"""
573-
fmt = mticker.IntegerFormatter(fmt, bits, unsigned)
574-
assert fmt(value) == expected
578+
formatter = mticker.IntegerFormatter(fmt, unsigned=unsigned, bits=bits)
579+
assert formatter(value) == expected
575580

576581

577582
def test_LinearScaleFormatter():
@@ -586,13 +591,15 @@ def baseFormatter(x, pos=None):
586591
outputs = ['{:0.2f}'.format(x) for x in np.arange(7.0)]
587592

588593
# Check a simple case
589-
fmt = mticker.LinearScaleFormatter(10, (1, 5), formatter=baseFormatter)
594+
fmt = mticker.LinearScaleFormatter(in_end=10, out_start=1, out_end=5,
595+
formatter=baseFormatter)
590596
for i, o in zip(inputs, outputs):
591597
print(i, fmt(i), o)
592598
assert fmt(i) == o
593599

594600
# Check a reverse mapping
595-
fmt = mticker.LinearScaleFormatter(10, (5, 1), formatter=baseFormatter)
601+
fmt = mticker.LinearScaleFormatter(in_end=10, out_start=5, out_end=1,
602+
formatter=baseFormatter)
596603
for i, o in zip(inputs, reversed(outputs)):
597604
print(i, fmt(i), o)
598605
assert fmt(i) == o

lib/matplotlib/ticker.py

+50-53
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,8 @@
154154
:class:`PercentFormatter`
155155
Format labels as a percentage
156156
157-
:class:`BinaryIntFormatter`
158-
Format labels as binary integers.
159-
160-
:class:`HexIntFormatter`
161-
Format labels as hexadecimal integers.
157+
:class:`IntegerFormatter`
158+
Extension of :class:`StrMethodFormatter` to support integer formats.
162159
163160
:class:`LinearScaleFormatter`
164161
Wrap another formatter to display transformed values.
@@ -204,9 +201,9 @@
204201
'LogFormatterExponent', 'LogFormatterMathtext',
205202
'IndexFormatter', 'LogFormatterSciNotation',
206203
'LogitFormatter', 'EngFormatter', 'PercentFormatter',
207-
'LinearScaleFormatter', 'BinaryIntFormatter',
208-
'HexIntFormatter', 'Locator', 'IndexLocator', 'FixedLocator',
209-
'NullLocator', 'LinearLocator', 'LogLocator', 'AutoLocator',
204+
'LinearScaleFormatter', 'IntegerFormatter', 'Locator',
205+
'IndexLocator', 'FixedLocator', 'NullLocator',
206+
'LinearLocator', 'LogLocator', 'AutoLocator',
210207
'MultipleLocator', 'MaxNLocator', 'AutoMinorLocator',
211208
'SymmetricalLogLocator', 'LogitLocator')
212209

@@ -1335,21 +1332,21 @@ def transform(self, x):
13351332
"""
13361333
Ensures that `x` is an integer.
13371334
1338-
If `self.unsigned` is `True`, then the integer is treated as a
1339-
positive number of size `self.bits`. If `self.bits` is `None`,
1340-
the number of bits is taken as `(-x).bit_length() - 1`.
1335+
If in unsigned mode, the integer is treated as a positive number
1336+
with the specified number of bits. The default number of bits is
1337+
taken as `(-x).bit_length() + 1`.
13411338
13421339
Returns the transformed number.
13431340
"""
13441341
x = int(x)
13451342
if x < 0 and self._unsigned:
1346-
bits = (-x).bit_length() if self._bits is None else self._bits
1343+
bits = (-x).bit_length() + 1 if self._bits is None else self._bits
13471344
x = x & ((1 << bits) - 1)
13481345
return x
13491346

13501347
def __call__(self, x, pos=None):
1351-
return super(IntegerFormatter, self).__call__(x=self.transform(x),
1352-
pos=pos)
1348+
if x < 0: self.p = None
1349+
return super(IntegerFormatter, self).__call__(self.transform(x), pos)
13531350

13541351

13551352
class LinearScaleFormatter(Formatter):
@@ -1360,67 +1357,67 @@ class LinearScaleFormatter(Formatter):
13601357
This formatter can use any other formatter to actually render the
13611358
ticks.
13621359
1363-
inRef: number or 2-element iterable
1364-
Bounds on the input range to match to the output range. These
1365-
numbers do not actually restrict the input in any way. They are
1366-
just reference points. If the range is a scalar, it will be
1367-
interpreted as ``(0, inRef)``.
1368-
outRef: number or 2-element iterable
1369-
Bounds on the output range to match to the input range. These
1370-
numbers do not actually restrict the output in any way. They are
1371-
just reference points. If the range is a scalar, it will be
1372-
interpreted as ``(0, outRef)``.
1373-
formatter: matplotlib.ticker.Formatter
1360+
in_start : number
1361+
Reference point on the input domain that matches `out_start` in
1362+
the output range. This number does not restrict the domain in
1363+
any way. Defaults to 0.0.
1364+
in_end : number
1365+
Reference point on the input domain that matches `out_end` in
1366+
the output range. This number does not restrict the domain in
1367+
any way. Defaults to 1.0.
1368+
out_start : number
1369+
Reference point on the output range that matches `in_start` in
1370+
the input domain. This number does not restrict the range in
1371+
any way. Defaults to 0.0.
1372+
out_end : number
1373+
Reference point on the output range that matches `in_end` in
1374+
the input domain. This number does not restrict the range in
1375+
any way. Defaults to 1.0.
1376+
formatter : matplotlib.ticker.Formatter
13741377
The instance to delegate the actual formatting of the
13751378
transformed value to. This does not have to be an instance of
13761379
``matplotlib.ticker.Formatter``. Any callable that accepts ``x``
1377-
and ``pos`` as arguments and returns a string will work.
1380+
and ``pos`` as arguments and returns a string will also work.
13781381
"""
1379-
def __init__(self, inRef=1.0, outRef=1.0, formatter=ScalarFormatter()):
1380-
def unpack(ref, name):
1381-
if np.iterable(ref):
1382-
ref = tuple(ref)
1383-
if len(ref) != 2:
1384-
raise ValueError('Expected 2-element iterable for `{}`, '
1385-
'got {}.'.format(name, len(ref)))
1386-
return ref
1387-
return 0, ref
1388-
1382+
def __init__(self, in_start=0.0, in_end=1.0, out_start=0.0, out_end=1.0,
1383+
formatter=ScalarFormatter()):
13891384
# All these values are retained for debugging/extension.
1390-
# Only the minima are used explicitly.
1391-
self.iMin, self.iMax = unpack(inRef, 'in')
1392-
self.oMin, self.oMax = unpack(outRef, 'out')
1393-
self.formatter = formatter
1385+
# Only the starts are used explicitly.
1386+
self._in_start = in_start
1387+
self._in_end = in_end
1388+
self._out_start = out_start
1389+
self._out_end = out_end
1390+
self._formatter = formatter
13941391

1395-
# Precomputing the values that are used in addition to the minima
1396-
self.iRange = self.iMax - self.iMin
1397-
self.oRange = self.oMax - self.oMin
1392+
# Precomputing the values that are used in addition to the starts
1393+
self._in_range = self._in_end - self._in_start
1394+
self._out_range = self._out_end - self._out_start
13981395

13991396
def transform(self, x):
14001397
"""
14011398
Transforms a value from the input scale to the output scale.
14021399
"""
1403-
return (x - self.iMin) / self.iRange * self.oRange + self.oMin
1400+
return (x - self._in_start) / self._in_range * self._out_range + self._out_start
14041401

14051402
def __call__(self, x, pos=None):
1406-
return self.formatter(self.transform(x), pos)
1403+
return self._formatter(self.transform(x), pos)
14071404

14081405
def set_axis(self, ax):
1409-
if hasattr(self.formatter, 'set_axis'):
1410-
self.formatter.set_axis(ax)
1406+
if hasattr(self._formatter, 'set_axis'):
1407+
self._formatter.set_axis(ax)
14111408

14121409
def get_offset(self):
1413-
if hasattr(self.formatter, 'get_axis'):
1414-
return self.formatter.get_offset()
1410+
if hasattr(self._formatter, 'get_axis'):
1411+
return self._formatter.get_offset()
14151412
return super(LinearScaleFormatter, self).get_offset()
14161413

14171414
def set_locs(self, locs):
1418-
if hasattr(self.formatter, 'set_locs'):
1419-
self.formatter.set_locs([self.transform(x) for x in locs])
1415+
if hasattr(self._formatter, 'set_locs'):
1416+
self._formatter.set_locs([self.transform(x) for x in locs])
14201417

14211418
def fix_minus(self, s):
1422-
if hasattr(self.formatter, 'fix_minus'):
1423-
return self.formatter.fix_minus(s)
1419+
if hasattr(self._formatter, 'fix_minus'):
1420+
return self._formatter.fix_minus(s)
14241421
return super(LinearScaleFormatter, self).fix_minus(s)
14251422

14261423

0 commit comments

Comments
 (0)