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

Skip to content

Commit 4d2de40

Browse files
committed
MaxNLocator: add validation, fix integer bug
1 parent 1c1fd38 commit 4d2de40

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_MaxNLocator_integer():
3232
test_value = np.array([-1, 0, 1, 2])
3333
assert_almost_equal(loc.tick_values(-0.1, 1.1), test_value)
3434

35-
test_value = np.array([-0.25, 0, 0.25, 0.5, 0.75, 1])
35+
test_value = np.array([-0.3, 0, 0.3, 0.6, 0.9, 1.2])
3636
assert_almost_equal(loc.tick_values(-0.1, 0.95), test_value)
3737

3838

lib/matplotlib/ticker.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,33 @@ def __init__(self, *args, **kwargs):
16851685
self.set_params(**self.default_params)
16861686
self.set_params(**kwargs)
16871687

1688+
@staticmethod
1689+
def _validate_steps(steps):
1690+
if not np.iterable(steps):
1691+
raise ValueError('steps argument must be a sequence of numbers '
1692+
'from 1 to 10')
1693+
steps = np.asarray(steps)
1694+
if np.any(np.diff(steps) <= 0):
1695+
raise ValueError('steps argument must be uniformly increasing')
1696+
if np.any((steps > 10) | (steps < 1)):
1697+
warnings.warn('Steps argument should be a sequence of numbers\n'
1698+
'increasing from 1 to 10, inclusive. Behavior with\n'
1699+
'values outside this range is undefined, and will\n'
1700+
'raise a ValueError in future versions of mpl.')
1701+
if steps[0] != 1:
1702+
steps = np.hstack((1, steps))
1703+
if steps[-1] != 10:
1704+
steps = np.hstack((steps, 10))
1705+
return steps
1706+
1707+
@staticmethod
1708+
def _staircase(steps):
1709+
# Make an extended staircase within which the needed
1710+
# step will be found. This is probably much larger
1711+
# than necessary.
1712+
flights = (0.1 * steps[:-1], steps, 10 * steps[1])
1713+
return np.hstack(flights)
1714+
16881715
def set_params(self, **kwargs):
16891716
"""Set parameters within this locator."""
16901717
if 'nbins' in kwargs:
@@ -1706,23 +1733,16 @@ def set_params(self, **kwargs):
17061733
if 'steps' in kwargs:
17071734
steps = kwargs['steps']
17081735
if steps is None:
1709-
self._steps = [1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10]
1736+
self._steps = np.array([1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10])
17101737
else:
1711-
if int(steps[-1]) != 10:
1712-
steps = list(steps)
1713-
steps.append(10)
1714-
self._steps = steps
1715-
# Make an extended staircase within which the needed
1716-
# step will be found. This is probably much larger
1717-
# than necessary.
1718-
flights = (0.1 * np.array(self._steps[:-1]),
1719-
self._steps,
1720-
[10 * self._steps[1]])
1721-
self._extended_steps = np.hstack(flights)
1738+
self._steps = self._validate_steps(steps)
1739+
self._extended_steps = self._staircase(self._steps)
17221740
if 'integer' in kwargs:
17231741
self._integer = kwargs['integer']
17241742
if self._integer:
1725-
self._steps = [n for n in self._steps if _divmod(n, 1)[1] < 0.001]
1743+
self._steps = np.array([n for n in self._steps
1744+
if _divmod(n, 1)[1] < 0.001])
1745+
self._extended_steps = self._staircase(self._steps)
17261746
if 'min_n_ticks' in kwargs:
17271747
self._min_n_ticks = max(1, kwargs['min_n_ticks'])
17281748

0 commit comments

Comments
 (0)