Skip to content

MaxNLocator 'steps' validation; and documentation. Closes #7578. #7586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/users/dflt_style_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,11 @@ or create a new `~matplotlib.ticker.MaxNLocator`::
import matplotlib.ticker as mticker
ax.set_major_locator(mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10])

The algorithm used by `~matplotlib.ticker.MaxNLocator` has been
improved, and this may change the choice of tick locations in some
cases. This also affects `~matplotlib.ticker.AutoLocator`, which
uses ``MaxNLocator`` internally.

For a log-scaled axis the default locator is the
`~matplotlib.ticker.LogLocator`. Previously the maximum number
of ticks was set to 15, and could not be changed. Now there is a
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_MaxNLocator_integer():
test_value = np.array([-1, 0, 1, 2])
assert_almost_equal(loc.tick_values(-0.1, 1.1), test_value)

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


Expand Down
46 changes: 33 additions & 13 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,33 @@ def __init__(self, *args, **kwargs):
self.set_params(**self.default_params)
self.set_params(**kwargs)

@staticmethod
def _validate_steps(steps):
if not np.iterable(steps):
raise ValueError('steps argument must be a sequence of numbers '
'from 1 to 10')
steps = np.asarray(steps)
if np.any(np.diff(steps) <= 0):
raise ValueError('steps argument must be uniformly increasing')
if steps[-1] > 10 or steps[0] < 1:
warnings.warn('Steps argument should be a sequence of numbers\n'
'increasing from 1 to 10, inclusive. Behavior with\n'
'values outside this range is undefined, and will\n'
'raise a ValueError in future versions of mpl.')
if steps[0] != 1:
steps = np.hstack((1, steps))
if steps[-1] != 10:
steps = np.hstack((steps, 10))
return steps

@staticmethod
def _staircase(steps):
# Make an extended staircase within which the needed
# step will be found. This is probably much larger
# than necessary.
flights = (0.1 * steps[:-1], steps, 10 * steps[1])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I never noticed this on the previous PR, but why is the third entry ten times the second step, and not ten times the second-to-the-last steps in the same way as the first element?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If steps is [1, 2, 5, 10], then we want extended steps to be [0.1, 0.2, 0.5, 1, 2, 5, 10, 20].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so I was wondering why 20 and not all the way to 100? Is it just redundant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The strategy is to start with the largest step and work down to smaller ones, finding the smallest step that doesn't lead to too many intervals. Everything is first scaled by a power of 10 based on an initial estimate. Normally, this means that starting at the 10 would be adequate, but occasionally one step larger is needed, hence the inclusion of the next step past 10. I couldn't see any circumstance that would require starting with a larger step than that, so I stopped there.

return np.hstack(flights)

def set_params(self, **kwargs):
"""Set parameters within this locator."""
if 'nbins' in kwargs:
Expand All @@ -1706,23 +1733,16 @@ def set_params(self, **kwargs):
if 'steps' in kwargs:
steps = kwargs['steps']
if steps is None:
self._steps = [1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10]
self._steps = np.array([1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10])
else:
if int(steps[-1]) != 10:
steps = list(steps)
steps.append(10)
self._steps = steps
# Make an extended staircase within which the needed
# step will be found. This is probably much larger
# than necessary.
flights = (0.1 * np.array(self._steps[:-1]),
self._steps,
[10 * self._steps[1]])
self._extended_steps = np.hstack(flights)
self._steps = self._validate_steps(steps)
self._extended_steps = self._staircase(self._steps)
if 'integer' in kwargs:
self._integer = kwargs['integer']
if self._integer:
self._steps = [n for n in self._steps if _divmod(n, 1)[1] < 0.001]
self._steps = np.array([n for n in self._steps
if _divmod(n, 1)[1] < 0.001])
self._extended_steps = self._staircase(self._steps)
if 'min_n_ticks' in kwargs:
self._min_n_ticks = max(1, kwargs['min_n_ticks'])

Expand Down