Skip to content

Commit 892fc5f

Browse files
committed
MaxNLocator only returns points within bounds.
e.g. when the xlims are -0.5 .. 10.5, return 0 .. 10 instead of -1 .. 11. This implies dropping the (unused and deprecated-in-comment) "trim" keyword argument. Also slightly clean up the implementation of scale_range. Preliminary work on #5738.
1 parent c708029 commit 892fc5f

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

lib/matplotlib/ticker.py

+15-28
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
from matplotlib import rcParams
162162
from matplotlib import cbook
163163
from matplotlib import transforms as mtransforms
164+
from matplotlib.cbook import mplDeprecation
164165

165166
import warnings
166167

@@ -1323,23 +1324,12 @@ def view_limits(self, dmin, dmax):
13231324

13241325

13251326
def scale_range(vmin, vmax, n=1, threshold=100):
1326-
dv = abs(vmax - vmin)
1327-
if dv == 0: # maxabsv == 0 is a special case of this.
1328-
return 1.0, 0.0
1329-
# Note: this should never occur because
1330-
# vmin, vmax should have been checked by nonsingular(),
1331-
# and spread apart if necessary.
1332-
meanv = 0.5 * (vmax + vmin)
1333-
if abs(meanv) / dv < threshold:
1334-
offset = 0
1335-
elif meanv > 0:
1336-
ex = divmod(math.log10(meanv), 1)[0]
1337-
offset = 10 ** ex
1338-
else:
1339-
ex = divmod(math.log10(-meanv), 1)[0]
1340-
offset = -10 ** ex
1341-
ex = divmod(math.log10(dv / n), 1)[0]
1342-
scale = 10 ** ex
1327+
dv = abs(vmax - vmin) # > 0 as nonsingular is called before.
1328+
meanv = (vmax + vmin) / 2
1329+
offset = (math.copysign(10 ** (math.log10(abs(meanv)) // 1), meanv)
1330+
if abs(meanv) / dv >= threshold
1331+
else 0)
1332+
scale = 10 ** (math.log10(dv / n) // 1)
13431333
return scale, offset
13441334

13451335

@@ -1349,7 +1339,6 @@ class MaxNLocator(Locator):
13491339
"""
13501340
default_params = dict(nbins=10,
13511341
steps=None,
1352-
trim=True,
13531342
integer=False,
13541343
symmetric=False,
13551344
prune=None)
@@ -1385,9 +1374,6 @@ def __init__(self, *args, **kwargs):
13851374
will be removed. If prune==None, no ticks will be removed.
13861375
13871376
"""
1388-
# I left "trim" out; it defaults to True, and it is not
1389-
# clear that there is any use case for False, so we may
1390-
# want to remove that kwarg. EF 2010/04/18
13911377
if args:
13921378
kwargs['nbins'] = args[0]
13931379
if len(args) > 1:
@@ -1403,7 +1389,8 @@ def set_params(self, **kwargs):
14031389
if self._nbins != 'auto':
14041390
self._nbins = int(self._nbins)
14051391
if 'trim' in kwargs:
1406-
self._trim = kwargs['trim']
1392+
warnings.warn("The 'trim' keyword has no effect anymore",
1393+
mplDeprecation)
14071394
if 'integer' in kwargs:
14081395
self._integer = kwargs['integer']
14091396
if 'symmetric' in kwargs:
@@ -1446,14 +1433,14 @@ def bin_boundaries(self, vmin, vmax):
14461433
if step < scaled_raw_step:
14471434
continue
14481435
step *= scale
1449-
best_vmin = step * divmod(vmin, step)[0]
1436+
best_vmin = vmin // step * step
14501437
best_vmax = best_vmin + step * nbins
1451-
if (best_vmax >= vmax):
1438+
if best_vmax >= vmax:
14521439
break
1453-
if self._trim:
1454-
extra_bins = int(divmod((best_vmax - vmax), step)[0])
1455-
nbins -= extra_bins
1456-
return (np.arange(nbins + 1) * step + best_vmin + offset)
1440+
1441+
bounds = np.arange(nbins + 1) * step + best_vmin
1442+
bounds = bounds[(bounds >= vmin) & (bounds <= vmax)]
1443+
return bounds + offset
14571444

14581445
def __call__(self):
14591446
vmin, vmax = self.axis.get_view_interval()

0 commit comments

Comments
 (0)