Skip to content

Clean + comment MaxNLocator #25166

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
Feb 10, 2023
Merged
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
41 changes: 21 additions & 20 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1978,12 +1978,12 @@ def __init__(self, nbins=None, **kwargs):
automatically determined based on the length of the axis.

steps : array-like, optional
Sequence of nice numbers starting with 1 and ending with 10;
e.g., [1, 2, 4, 5, 10], where the values are acceptable
tick multiples. i.e. for the example, 20, 40, 60 would be
an acceptable set of ticks, as would 0.4, 0.6, 0.8, because
they are multiples of 2. However, 30, 60, 90 would not
be allowed because 3 does not appear in the list of steps.
Sequence of acceptable tick multiples, starting with 1 and
ending with 10. For example, if ``steps=[1, 2, 4, 5, 10]``,
``20, 40, 60`` or ``0.4, 0.6, 0.8`` would be possible
sets of ticks because they are multiples of 2.
``30, 60, 90`` would not be generated because 3 does not
appear in this example list of steps.

integer : bool, default: False
If True, ticks will take only integer values, provided at least
Expand Down Expand Up @@ -2092,27 +2092,28 @@ def _raw_ticks(self, vmin, vmax):
scale, offset = scale_range(vmin, vmax, nbins)
_vmin = vmin - offset
_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.
raw_step = ((_vmax - _vmin) / nbins)
large_steps = steps >= raw_step
if mpl.rcParams['axes.autolimit_mode'] == 'round_numbers':
for istep in range(istep, len(steps)):
step = steps[istep]
best_vmin = (_vmin // step) * step
best_vmax = best_vmin + step * nbins
if best_vmax >= _vmax:
break

# This is an upper limit; move to smaller steps if necessary.
for istep in reversed(range(istep + 1)):
step = steps[istep]
# Classic round_numbers mode may require a larger step.
# Get first multiple of steps that are <= _vmin
floored_vmins = (_vmin // steps) * steps
floored_vmaxs = floored_vmins + steps * nbins
large_steps = large_steps & (floored_vmaxs >= _vmax)

# Find index of smallest large step
istep = np.nonzero(large_steps)[0][0]

# Start at smallest of the steps greater than the raw step, and check
# if it provides enough ticks. If not, work backwards through
# smaller steps until one is found that provides enough ticks.
for step in steps[:istep+1][::-1]:

if (self._integer and
np.floor(_vmax) - np.ceil(_vmin) >= self._min_n_ticks - 1):
Expand Down