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 39efb99

Browse filesBrowse files
committed
Slightly more efficient impl.; more tests.
1 parent 5726980 commit 39efb99
Copy full SHA for 39efb99

File tree

2 files changed

+11
-16
lines changed
Filter options

2 files changed

+11
-16
lines changed

‎lib/matplotlib/tests/test_ticker.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_ticker.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def check_offset_for(left, right, offset):
201201

202202
for left, right, offset in test_data:
203203
yield check_offset_for, left, right, offset
204+
yield check_offset_for, right, left, offset
204205

205206

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

‎lib/matplotlib/ticker.py

Copy file name to clipboardExpand all lines: lib/matplotlib/ticker.py
+10-16Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.