Skip to content

Commit 250a8bb

Browse files
authored
Clean + comment MaxNLocator (#25166)
* Clean + comment MaxNLocator * Put back comment * Improve variable name
1 parent 91d8f2e commit 250a8bb

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

lib/matplotlib/ticker.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,12 +1978,12 @@ def __init__(self, nbins=None, **kwargs):
19781978
automatically determined based on the length of the axis.
19791979
19801980
steps : array-like, optional
1981-
Sequence of nice numbers starting with 1 and ending with 10;
1982-
e.g., [1, 2, 4, 5, 10], where the values are acceptable
1983-
tick multiples. i.e. for the example, 20, 40, 60 would be
1984-
an acceptable set of ticks, as would 0.4, 0.6, 0.8, because
1985-
they are multiples of 2. However, 30, 60, 90 would not
1986-
be allowed because 3 does not appear in the list of steps.
1981+
Sequence of acceptable tick multiples, starting with 1 and
1982+
ending with 10. For example, if ``steps=[1, 2, 4, 5, 10]``,
1983+
``20, 40, 60`` or ``0.4, 0.6, 0.8`` would be possible
1984+
sets of ticks because they are multiples of 2.
1985+
``30, 60, 90`` would not be generated because 3 does not
1986+
appear in this example list of steps.
19871987
19881988
integer : bool, default: False
19891989
If True, ticks will take only integer values, provided at least
@@ -2092,27 +2092,28 @@ def _raw_ticks(self, vmin, vmax):
20922092
scale, offset = scale_range(vmin, vmax, nbins)
20932093
_vmin = vmin - offset
20942094
_vmax = vmax - offset
2095-
raw_step = (_vmax - _vmin) / nbins
20962095
steps = self._extended_steps * scale
20972096
if self._integer:
20982097
# For steps > 1, keep only integer values.
20992098
igood = (steps < 1) | (np.abs(steps - np.round(steps)) < 0.001)
21002099
steps = steps[igood]
21012100

2102-
istep = np.nonzero(steps >= raw_step)[0][0]
2103-
2104-
# Classic round_numbers mode may require a larger step.
2101+
raw_step = ((_vmax - _vmin) / nbins)
2102+
large_steps = steps >= raw_step
21052103
if mpl.rcParams['axes.autolimit_mode'] == 'round_numbers':
2106-
for istep in range(istep, len(steps)):
2107-
step = steps[istep]
2108-
best_vmin = (_vmin // step) * step
2109-
best_vmax = best_vmin + step * nbins
2110-
if best_vmax >= _vmax:
2111-
break
2112-
2113-
# This is an upper limit; move to smaller steps if necessary.
2114-
for istep in reversed(range(istep + 1)):
2115-
step = steps[istep]
2104+
# Classic round_numbers mode may require a larger step.
2105+
# Get first multiple of steps that are <= _vmin
2106+
floored_vmins = (_vmin // steps) * steps
2107+
floored_vmaxs = floored_vmins + steps * nbins
2108+
large_steps = large_steps & (floored_vmaxs >= _vmax)
2109+
2110+
# Find index of smallest large step
2111+
istep = np.nonzero(large_steps)[0][0]
2112+
2113+
# Start at smallest of the steps greater than the raw step, and check
2114+
# if it provides enough ticks. If not, work backwards through
2115+
# smaller steps until one is found that provides enough ticks.
2116+
for step in steps[:istep+1][::-1]:
21162117

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

0 commit comments

Comments
 (0)