8000 Simplify decade up- and down-rounding, and symmetrize expansion of degenerate log scales. by anntzer · Pull Request #13413 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Simplify decade up- and down-rounding, and symmetrize expansion of degenerate log scales. #13413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/next_api_changes/2019-02-13-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Deprecations
````````````
``ticker.decade_up`` and ``ticker.decade_down`` are deprecated.

Autoscaling changes
```````````````````
On log-axes where a single value is plotted at a "full" decade (1, 10, 100,
etc.), the autoscaling now expands the axis symmetrically around that point,
instead of adding a decade only to the right.
83 changes: 60 additions & 23 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,7 @@ def view_limits(self, dmin, dmax):
return dmin, dmax


@cbook.deprecated("3.1")
def decade_down(x, base=10):
'floor x to the nearest lower decade'
if x == 0.0:
Expand All @@ -2069,6 +2070,7 @@ def decade_down(x, base=10):
return base ** lx


@cbook.deprecated("3.1")
def decade_up(x, base=10):
'ceil x to the nearest higher decade'
if x == 0.0:
Expand All @@ -2094,6 +2096,56 @@ def is_decade(x, base=10):
return is_close_to_int(lx)


def _decade_less_equal(x, base):
"""
Return the largest integer power of *base* that's less or equal to *x*.

If *x* is negative, the exponent will be *greater*.
"""
return (x if x == 0 else
-_decade_greater_equal(-x, base) if x < 0 else
base ** np.floor(np.log(x) / np.log(base)))


def _decade_greater_equal(x, base):
"""
Return the smallest integer power of *base* that's greater or equal to *x*.

If *x* is negative, the exponent will be *smaller*.
"""
return (x if x == 0 else
-_decade_less_equal(-x, base) if x < 0 else
base ** np.ceil(np.log(x) / np.log(base)))


def _decade_less(x, base):
"""
Return the largest integer power of *base* that's less than *x*.

If *x* is negative, the exponent will be *greater*.
"""
if x < 0:
return -_decade_greater(-x, base)
less = _decade_less_equal(x, base)
if less == x:
less /= base
return less


def _decade_greater(x, base):
"""
Return the smallest integer power of *base* that's greater than *x*.

If *x* is negative, the exponent will be *smaller*.
"""
if x < 0:
return -_decade_less(-x, base)
greater = _decade_greater_equal(x, base)
if greater == x:
greater *= base
return greater


def is_close_to_int(x):
return abs(x - np.round(x)) < 1e-10

Expand Down Expand Up @@ -2271,10 +2323,8 @@ def view_limits(self, vmin, vmax):
vmin = b ** (vmax - self.numdecs)

if rcParams['axes.autolimit_mode'] == 'round_numbers':
if not is_decade(vmin, self._base):
vmin = decade_down(vmin, self._base)
if not is_decade(vmax, self._base):
vmax = decade_up(vmax, self._base)
vmin = _decade_less_equal(vmin, self._base)
vmax = _decade_greater_equal(vmax, self._base)

return vmin, vmax

Expand All @@ -2296,8 +2346,8 @@ def nonsingular(self, vmin, vmax):
if vmin <= 0:
vmin = minpos
if vmin == vmax:
vmin = decade_down(vmin, self._base)
vmax = decade_up(vmax, self._base)
vmin = _decade_less(vmin, self._base)
vmax = _decade_greater(vmax, self._base)
return vmin, vmax


Expand Down Expand Up @@ -2451,24 +2501,11 @@ def view_limits(self, vmin, vmax):
vmin, vmax = vmax, vmin

if rcParams['axes.autolimit_mode'] == 'round_numbers':
if not is_decade(abs(vmin), b):
if vmin < 0:
vmin = -decade_up(-vmin, b)
else:
vmin = decade_down(vmin, b)
if not is_decade(abs(vmax), b):
if vmax < 0:
vmax = -decade_down(-vmax, b)
else:
vmax = decade_up(vmax, b)

vmin = _decade_less_equal(vmin, b)
vmax = _decade_greater_equal(vmax, b)
if vmin == vmax:
if vmin < 0:
vmin = -decade_up(-vmin, b)
vmax = -decade_down(-vmax, b)
else:
vmin = decade_down(vmin, b)
vmax = decade_up(vmax, b)
vmin = _decade_less(vmin, b)
vmax = _decade_greater(vmax, b)

result = mtransforms.nonsingular(vmin, vmax)
return result
Expand Down
0