Skip to content

Commit

Permalink
Merge pull request #6192 from jenshnielsen/tighter-axes-limits
Browse files Browse the repository at this point in the history
More precise choice of axes limits.
  • Loading branch information
tacaswell committed Mar 20, 2016
2 parents b8d8b1b + 6d23c8e commit 996f3ec
Show file tree
Hide file tree
Showing 23 changed files with 3,495 additions and 3,593 deletions.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
436 changes: 194 additions & 242 deletions lib/matplotlib/tests/baseline_images/test_axes/autoscale_tiny_range.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
1,012 changes: 506 additions & 506 deletions lib/matplotlib/tests/baseline_images/test_axes/errorbar_mixed.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
234 changes: 107 additions & 127 deletions lib/matplotlib/tests/baseline_images/test_axes/formatter_large_small.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions lib/matplotlib/tests/test_axes.py
Expand Up @@ -24,7 +24,7 @@
from matplotlib.testing.decorators import image_comparison, cleanup
import matplotlib.pyplot as plt
import matplotlib.markers as mmarkers
from numpy.testing import assert_array_equal
from numpy.testing import assert_allclose, assert_array_equal
import warnings
from matplotlib.cbook import IgnoredKeywordWarning

Expand Down Expand Up @@ -3725,8 +3725,7 @@ def test_vline_limit():
ax.axvline(0.5)
ax.plot([-0.1, 0, 0.2, 0.1])
(ymin, ymax) = ax.get_ylim()
assert ymin == -0.1
assert ymax == 0.25
assert_allclose(ax.get_ylim(), (-.1, .2))


@cleanup
Expand Down
81 changes: 43 additions & 38 deletions lib/matplotlib/ticker.py
Expand Up @@ -161,13 +161,23 @@
from matplotlib import rcParams
from matplotlib import cbook
from matplotlib import transforms as mtransforms
from matplotlib.cbook import mplDeprecation

import warnings

if six.PY3:
long = int


# Work around numpy/numpy#6127.
def _divmod(x, y):
if isinstance(x, np.generic):
x = x.item()
if isinstance(y, np.generic):
y = y.item()
return six.moves.builtins.divmod(x, y)


def _mathdefault(s):
"""
For backward compatibility, in classic mode we display
Expand Down Expand Up @@ -1221,7 +1231,7 @@ def view_limits(self, vmin, vmax):
vmax += 1

if rcParams['axes.autolimit_mode'] == 'round_numbers':
exponent, remainder = divmod(math.log10(vmax - vmin), 1)
exponent, remainder = _divmod(math.log10(vmax - vmin), 1)
if remainder < 0.5:
exponent -= 1
scale = 10 ** (-exponent)
Expand All @@ -1247,30 +1257,30 @@ def __init__(self, base):

def lt(self, x):
'return the largest multiple of base < x'
d, m = divmod(x, self._base)
d, m = _divmod(x, self._base)
if closeto(m, 0) and not closeto(m / self._base, 1):
return (d - 1) * self._base
return d * self._base

def le(self, x):
'return the largest multiple of base <= x'
d, m = divmod(x, self._base)
d, m = _divmod(x, self._base)
if closeto(m / self._base, 1): # was closeto(m, self._base)
#looks like floating point error
return (d + 1) * self._base
return d * self._base

def gt(self, x):
'return the smallest multiple of base > x'
d, m = divmod(x, self._base)
d, m = _divmod(x, self._base)
if closeto(m / self._base, 1):
#looks like floating point error
return (d + 2) * self._base
return (d + 1) * self._base

def ge(self, x):
'return the smallest multiple of base >= x'
d, m = divmod(x, self._base)
d, m = _divmod(x, self._base)
if closeto(m, 0) and not closeto(m / self._base, 1):
return d * self._base
return (d + 1) * self._base
Expand Down Expand Up @@ -1326,23 +1336,13 @@ 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)
dv = abs(vmax - vmin) # > 0 as nonsingular is called before.
meanv = (vmax + vmin) / 2
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
offset = math.copysign(10 ** (math.log10(abs(meanv)) // 1), meanv)
scale = 10 ** (math.log10(dv / n) // 1)
return scale, offset


Expand All @@ -1352,7 +1352,6 @@ class MaxNLocator(Locator):
"""
default_params = dict(nbins=10,
steps=None,
trim=True,
integer=False,
symmetric=False,
prune=None)
Expand Down Expand Up @@ -1388,9 +1387,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 @@ -1406,7 +1402,9 @@ 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 since version 2.0.",
mplDeprecation)
if 'integer' in kwargs:
self._integer = kwargs['integer']
if 'symmetric' in kwargs:
Expand All @@ -1429,9 +1427,9 @@ def set_params(self, **kwargs):
if 'integer' in kwargs:
self._integer = kwargs['integer']
if self._integer:
self._steps = [n for n in self._steps if divmod(n, 1)[1] < 0.001]
self._steps = [n for n in self._steps if _divmod(n, 1)[1] < 0.001]

def bin_boundaries(self, vmin, vmax):
def _raw_ticks(self, vmin, vmax):
nbins = self._nbins
if nbins == 'auto':
nbins = max(min(self.axis.get_tick_space(), 9), 1)
Expand All @@ -1449,23 +1447,30 @@ 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)

# More than nbins may be required, e.g. vmin, vmax = -4.1, 4.1 gives
# nbins=9 but 10 bins are actually required after rounding. So we just
# create the bins that span the range we need instead.
low = round(Base(step).le(vmin - best_vmin) / step)
high = round(Base(step).ge(vmax - best_vmin) / step)
return np.arange(low, high + 1) * step + best_vmin + offset

@cbook.deprecated("2.0")
def bin_boundaries(self, vmin, vmax):
return self._raw_ticks(vmin, vmax)

def __call__(self):
vmin, vmax = self.axis.get_view_interval()
return self.tick_values(vmin, vmax)

def tick_values(self, vmin, vmax):
vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander=1e-13,
tiny=1e-14)
locs = self.bin_boundaries(vmin, vmax)
vmin, vmax = mtransforms.nonsingular(
vmin, vmax, expander=1e-13, tiny=1e-14)
locs = self._raw_ticks(vmin, vmax)
prune = self._prune
if prune == 'lower':
locs = locs[1:]
Expand All @@ -1482,11 +1487,11 @@ def view_limits(self, dmin, dmax):
dmin = -maxabs
dmax = maxabs

dmin, dmax = mtransforms.nonsingular(dmin, dmax, expander=1e-12,
tiny=1.e-13)
dmin, dmax = mtransforms.nonsingular(
dmin, dmax, expander=1e-12, tiny=1e-13)

if rcParams['axes.autolimit_mode'] == 'round_numbers':
return np.take(self.bin_boundaries(dmin, dmax), [0, -1])
return self._raw_ticks(dmin, dmax)[[0, -1]]
else:
return dmin, dmax

Expand Down
Binary file modified lib/mpl_toolkits/tests/baseline_images/test_mplot3d/quiver3d.pdf
Binary file not shown.
Binary file modified lib/mpl_toolkits/tests/baseline_images/test_mplot3d/quiver3d.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 996f3ec

Please sign in to comment.