Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 032beeb

Browse filesBrowse files
tacaswellMeeseeksDev[bot]
authored andcommitted
Backport PR #13413: Simplify decade up- and down-rounding, and symmetrize expansion of degenerate log scales.
1 parent 2797c8c commit 032beeb
Copy full SHA for 032beeb

File tree

Expand file treeCollapse file tree

2 files changed

+69
-23
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+69
-23
lines changed
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Deprecations
2+
````````````
3+
``ticker.decade_up`` and ``ticker.decade_down`` are deprecated.
4+
5+
Autoscaling changes
6+
```````````````````
7+
On log-axes where a single value is plotted at a "full" decade (1, 10, 100,
8+
etc.), the autoscaling now expands the axis symmetrically around that point,
9+
instead of adding a decade only to the right.

‎lib/matplotlib/ticker.py

Copy file name to clipboardExpand all lines: lib/matplotlib/ticker.py
+60-23Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,7 @@ def view_limits(self, dmin, dmax):
20612061
return dmin, dmax
20622062

20632063

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

20712072

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

20962098

2099+
def _decade_less_equal(x, base):
2100+
"""
2101+
Return the largest integer power of *base* that's less or equal to *x*.
2102+
2103+
If *x* is negative, the exponent will be *greater*.
2104+
"""
2105+
return (x if x == 0 else
2106+
-_decade_greater_equal(-x, base) if x < 0 else
2107+
base ** np.floor(np.log(x) / np.log(base)))
2108+
2109+
2110+
def _decade_greater_equal(x, base):
2111+
"""
2112+
Return the smallest integer power of *base* that's greater or equal to *x*.
2113+
2114+
If *x* is negative, the exponent will be *smaller*.
2115+
"""
2116+
return (x if x == 0 else
2117+
-_decade_less_equal(-x, base) if x < 0 else
2118+
base ** np.ceil(np.log(x) / np.log(base)))
2119+
2120+
2121+
def _decade_less(x, base):
2122+
"""
2123+
Return the largest integer power of *base* that's less than *x*.
2124+
2125+
If *x* is negative, the exponent will be *greater*.
2126+
"""
2127+
if x < 0:
2128+
return -_decade_greater(-x, base)
2129+
less = _decade_less_equal(x, base)
2130+
if less == x:
2131+
less /= base
2132+
return less
2133+
2134+
2135+
def _decade_greater(x, base):
2136+
"""
2137+
Return the smallest integer power of *base* that's greater than *x*.
2138+
2139+
If *x* is negative, the exponent will be *smaller*.
2140+
"""
2141+
if x < 0:
2142+
return -_decade_less(-x, base)
2143+
greater = _decade_greater_equal(x, base)
2144+
if greater == x:
2145+
greater *= base
2146+
return greater
2147+
2148+
20972149
def is_close_to_int(x):
20982150
return abs(x - np.round(x)) < 1e-10
20992151

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

22732325
if rcParams['axes.autolimit_mode'] == 'round_numbers':
2274-
if not is_decade(vmin, self._base):
2275-
vmin = decade_down(vmin, self._base)
2276-
if not is_decade(vmax, self._base):
2277-
vmax = decade_up(vmax, self._base)
2326+
vmin = _decade_less_equal(vmin, self._base)
2327+
vmax = _decade_greater_equal(vmax, self._base)
22782328

22792329
return vmin, vmax
22802330

@@ -2296,8 +2346,8 @@ def nonsingular(self, vmin, vmax):
22962346
if vmin <= 0:
22972347
vmin = minpos
22982348
if vmin == vmax:
2299-
vmin = decade_down(vmin, self._base)
2300-
vmax = decade_up(vmax, self._base)
2349+
vmin = _decade_less(vmin, self._base)
2350+
vmax = _decade_greater(vmax, self._base)
23012351
return vmin, vmax
23022352

23032353

@@ -2451,24 +2501,11 @@ def view_limits(self, vmin, vmax):
24512501
vmin, vmax = vmax, vmin
24522502

24532503
if rcParams['axes.autolimit_mode'] == 'round_numbers':
2454-
if not is_decade(abs(vmin), b):
2455-
if vmin < 0:
2456-
vmin = -decade_up(-vmin, b)
2457-
else:
2458-
vmin = decade_down(vmin, b)
2459-
if not is_decade(abs(vmax), b):
2460-
if vmax < 0:
2461-
vmax = -decade_down(-vmax, b)
2462-
else:
2463-
vmax = decade_up(vmax, b)
2464-
2504+
vmin = _decade_less_equal(vmin, b)
2505+
vmax = _decade_greater_equal(vmax, b)
24652506
if vmin == vmax:
2466-
if vmin < 0:
2467-
vmin = -decade_up(-vmin, b)
2468-
vmax = -decade_down(-vmax, b)
2469-
else:
2470-
vmin = decade_down(vmin, b)
2471-
vmax = decade_up(vmax, b)
2507+
vmin = _decade_less(vmin, b)
2508+
vmax = _decade_greater(vmax, b)
24722509

24732510
result = mtransforms.nonsingular(vmin, vmax)
24742511
return result

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.