Skip to content

Commit

Permalink
MaxNLocator only returns points within bounds.
Browse files Browse the repository at this point in the history
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 matplotlib#5738.
  • Loading branch information
anntzer committed Dec 31, 2015
1 parent c708029 commit 892fc5f
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions lib/matplotlib/ticker.py
Expand Up @@ -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

Expand Down Expand Up @@ -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


Expand All @@ -1349,7 +1339,6 @@ class MaxNLocator(Locator):
"""
default_params = dict(nbins=10,
steps=None,
trim=True,
integer=False,
symmetric=False,
prune=None)
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 892fc5f

Please sign in to comment.