From 892fc5fa664b30df3dd07e5a540b7bace00d46c8 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 30 Dec 2015 08:17:10 -0800 Subject: [PATCH] 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. --- lib/matplotlib/ticker.py | 43 ++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index a2fb0a6e674a..e5afb6c04c4a 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -161,6 +161,7 @@ from matplotlib import rcParams from matplotlib import cbook from matplotlib import transforms as mtransforms +from matplotlib.cbook import mplDeprecation import warnings @@ -1323,23 +1324,12 @@ def view_limits(self, dmin, dmax): def scale_range(vmin, vmax, n=1, threshold=100): - dv = abs(vmax - vmin) - if dv == 0: # maxabsv == 0 is a special case of this. - return 1.0, 0.0 - # Note: this should never occur because - # vmin, vmax should have been checked by nonsingular(), - # and spread apart if necessary. - meanv = 0.5 * (vmax + vmin) - if abs(meanv) / dv < threshold: - offset = 0 - elif meanv > 0: - ex = divmod(math.log10(meanv), 1)[0] - offset = 10 ** ex - else: - ex = divmod(math.log10(-meanv), 1)[0] - offset = -10 ** ex - ex = divmod(math.log10(dv / n), 1)[0] - scale = 10 ** ex + dv = abs(vmax - vmin) # > 0 as nonsingular is called before. + meanv = (vmax + vmin) / 2 + offset = (math.copysign(10 ** (math.log10(abs(meanv)) // 1), meanv) + if abs(meanv) / dv >= threshold + else 0) + scale = 10 ** (math.log10(dv / n) // 1) return scale, offset @@ -1349,7 +1339,6 @@ class MaxNLocator(Locator): """ default_params = dict(nbins=10, steps=None, - trim=True, integer=False, symmetric=False, prune=None) @@ -1385,9 +1374,6 @@ def __init__(self, *args, **kwargs): will be removed. If prune==None, no ticks will be removed. """ - # I left "trim" out; it defaults to True, and it is not - # clear that there is any use case for False, so we may - # want to remove that kwarg. EF 2010/04/18 if args: kwargs['nbins'] = args[0] if len(args) > 1: @@ -1403,7 +1389,8 @@ def set_params(self, **kwargs): if self._nbins != 'auto': self._nbins = int(self._nbins) if 'trim' in kwargs: - self._trim = kwargs['trim'] + warnings.warn("The 'trim' keyword has no effect anymore", + mplDeprecation) if 'integer' in kwargs: self._integer = kwargs['integer'] if 'symmetric' in kwargs: @@ -1446,14 +1433,14 @@ def bin_boundaries(self, vmin, vmax): if step < scaled_raw_step: continue step *= scale - best_vmin = step * divmod(vmin, step)[0] + best_vmin = vmin // step * step best_vmax = best_vmin + step * nbins - if (best_vmax >= vmax): + if best_vmax >= vmax: break - if self._trim: - extra_bins = int(divmod((best_vmax - vmax), step)[0]) - nbins -= extra_bins - return (np.arange(nbins + 1) * step + best_vmin + offset) + + bounds = np.arange(nbins + 1) * step + best_vmin + bounds = bounds[(bounds >= vmin) & (bounds <= vmax)] + return bounds + offset def __call__(self): vmin, vmax = self.axis.get_view_interval()