Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c39a78

Browse files
committed
Slightly more efficient impl.; more tests.
1 parent f37fd3d commit 0c39a78

2 files changed

Lines changed: 13 additions & 16 deletions

File tree

lib/matplotlib/tests/test_ticker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ def check_offset_for(left, right, offset):
180180
(-100000.5, -99990.5, -100000),
181181
(1233999, 1234001, 1234000),
182182
(-1234001, -1233999, -1234000),
183+
(1, 1, 0),
184+
(123, 123, 123),
183185
# Test cases courtesy of @WeatherGod
184186
(.4538, .4578, .45),
185187
(3789.12, 3783.1, 3780),
@@ -201,6 +203,7 @@ def check_offset_for(left, right, offset):
201203

202204
for left, right, offset in test_data:
203205
yield check_offset_for, left, right, offset
206+
yield check_offset_for, right, left, offset
204207

205208

206209
def _logfe_helper(formatter, base, locs, i, expected_result):

lib/matplotlib/ticker.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
from matplotlib.externals import six
165165

166166
import decimal
167+
import itertools
167168
import locale
168169
import math
169170
import numpy as np
@@ -680,36 +681,29 @@ def _compute_offset(self):
680681
self.offset = 0
681682
return
682683
lmin, lmax = locs.min(), locs.max()
683-
# min, max comparing absolute values (we want division to round towards
684-
# zero so we work on absolute values).
685-
abs_min, abs_max = sorted([abs(float(lmin)), abs(float(lmax))])
686684
# Only use offset if there are at least two ticks and every tick has
687685
# the same sign.
688686
if lmin == lmax or lmin <= 0 <= lmax:
689687
self.offset = 0
690688
return
689+
# min, max comparing absolute values (we want division to round towards
690+
# zero so we work on absolute values).
691+
abs_min, abs_max = sorted([abs(float(lmin)), abs(float(lmax))])
691692
sign = math.copysign(1, lmin)
692693
# What is the smallest power of ten such that abs_min and abs_max are
693694
# equal up to that precision?
694695
# Note: Internally using oom instead of 10 ** oom avoids some numerical
695696
# accuracy issues.
696-
oom = math.ceil(math.log10(abs_max))
697-
while True:
698-
if abs_min // 10 ** oom != abs_max // 10 ** oom:
699-
oom += 1
700-
break
701-
oom -= 1
697+
oom_max = math.ceil(math.log10(abs_max))
698+
oom = 1 + next(oom for oom in itertools.count(oom_max, -1)
699+
if abs_min // 10 ** oom != abs_max // 10 ** oom)
702700
if (abs_max - abs_min) / 10 ** oom <= 1e-2:
703701
# Handle the case of straddling a multiple of a large power of ten
704702
# (relative to the span).
705703
# What is the smallest power of ten such that abs_min and abs_max
706-
# at most 1 apart?
707-
oom = math.ceil(math.log10(abs_max))
708-
while True:
709-
if abs_max // 10 ** oom - abs_min // 10 ** oom > 1:
710-
oom += 1
711-
break
712-
oom -= 1
704+
# are no more than 1 apart at that precision?
705+
oom = 1 + next(oom for oom in itertools.count(oom_max, -1)
706+
if abs_max // 10 ** oom - abs_min // 10 ** oom > 1)
713707
# Only use offset if it saves at least two significant digits.
714708
self.offset = (sign * (abs_max // 10 ** oom) * 10 ** oom
715709
if abs_max // 10 ** oom >= 10

0 commit comments

Comments
 (0)