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

Skip to content

Commit 80de215

Browse files
committed
Slightly more efficient impl.; more tests.
1 parent 5726980 commit 80de215

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

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
@@ -155,6 +155,7 @@
155155
from matplotlib.externals import six
156156

157157
import decimal
158+
import itertools
158159
import locale
159160
import math
160161
import numpy as np
@@ -565,36 +566,29 @@ def _compute_offset(self):
565566
self.offset = 0
566567
return
567568
lmin, lmax = locs.min(), locs.max()
568-
# min, max comparing absolute values (we want division to round towards
569-
# zero so we work on absolute values).
570-
abs_min, abs_max = sorted([abs(float(lmin)), abs(float(lmax))])
571569
# Only use offset if there are at least two ticks and every tick has
572570
# the same sign.
573571
if lmin == lmax or lmin <= 0 <= lmax:
574572
self.offset = 0
575573
return
574+
# min, max comparing absolute values (we want division to round towards
575+
# zero so we work on absolute values).
576+
abs_min, abs_max = sorted([abs(float(lmin)), abs(float(lmax))])
576577
sign = math.copysign(1, lmin)
577578
# What is the smallest power of ten such that abs_min and abs_max are
578579
# equal up to that precision?
579580
# Note: Internally using oom instead of 10 ** oom avoids some numerical
580581
# accuracy issues.
581-
oom = math.ceil(math.log10(abs_max))
582-
while True:
583-
if abs_min // 10 ** oom != abs_max // 10 ** oom:
584-
oom += 1
585-
break
586-
oom -= 1
582+
oom_max = math.ceil(math.log10(abs_max))
583+
oom = 1 + next(oom for oom in itertools.count(oom_max, -1)
584+
if abs_min // 10 ** oom != abs_max // 10 ** oom)
587585
if (abs_max - abs_min) / 10 ** oom <= 1e-2:
588586
# Handle the case of straddling a multiple of a large power of ten
589587
# (relative to the span).
590588
# What is the smallest power of ten such that abs_min and abs_max
591-
# at most 1 apart?
592-
oom = math.ceil(math.log10(abs_max))
593-
while True:
594-
if abs_max // 10 ** oom - abs_min // 10 ** oom > 1:
595-
oom += 1
596-
break
597-
oom -= 1
589+
# are no more than 1 apart at that precision?
590+
oom = 1 + next(oom for oom in itertools.count(oom_max, -1)
591+
if abs_max // 10 ** oom - abs_min // 10 ** oom > 1)
598592
# Only use offset if it saves at least two significant digits.
599593
self.offset = (sign * (abs_max // 10 ** oom) * 10 ** oom
600594
if abs_max // 10 ** oom >= 10

0 commit comments

Comments
 (0)