8000 BUG: improve integer step selection in MaxNLocator by efiring · Pull Request #7770 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

BUG: improve integer step selection in MaxNLocator #7770

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 1 commit into from
Jan 9, 2017
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
6 changes: 5 additions & 1 deletion lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ 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.3, 0, 0.3, 0.6, 0.9, 1.2])
test_value = np.array([-0.25, 0, 0.25, 0.5, 0.75, 1.0])
assert_almost_equal(loc.tick_values(-0.1, 0.95), test_value)

loc = mticker.MaxNLocator(nbins=5, integer=True, steps=[1, 1.5, 5, 6, 10])
test_value = np.array([0, 15, 30, 45, 60])
assert_almost_equal(loc.tick_values(1, 55), test_value)


def test_LinearLocator():
loc = mticker.LinearLocator(numticks=3)
Expand Down
13 changes: 7 additions & 6 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,8 @@ def set_params(self, **kwargs):
raise ValueError(
"prune must be 'upper', 'lower', 'both', or None")
self._prune = prune
if 'min_n_ticks' in kwargs:
self._min_n_ticks = max(1, kwargs['min_n_ticks'])
if 'steps' in kwargs:
steps = kwargs['steps']
if steps is None:
Expand All @@ -1845,12 +1847,6 @@ def set_params(self, **kwargs):
self._extended_steps = self._staircase(self._steps)
if 'integer' in kwargs:
self._integer = kwargs['integer']
if self._integer:
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'])

def _raw_ticks(self, vmin, vmax):
if self._nbins == 'auto':
Expand All @@ -1867,6 +1863,11 @@ def _raw_ticks(self, vmin, vmax):
_vmax = vmax - offset
raw_step = (vmax - vmin) / nbins
steps = self._extended_steps * scale
if self._integer:
# For steps > 1, keep only integer values.
igood = (steps < 1) | (np.abs(steps - np.round(steps)) < 0.001)
steps = steps[igood]

istep = np.nonzero(steps >= raw_step)[0][0]

# Classic round_numbers mode may require a larger step.
Expand Down
0