Skip to content

Commit 5f69ed8

Browse files
committed
Better choice of offset-text.
The axis offset text is choosen as follows: xlims => offsettext 123, 189 => 0 12341, 12349 => 12340 99999.5, 100010.5 => 100000 # (also a test for matplotlib#5780) 99990.5, 100000.5 => 100000 1233999, 1234001 => 1234000 (and the same for negative limits). See matplotlib#5755.
1 parent 9c690ed commit 5f69ed8

2 files changed

Lines changed: 83 additions & 19 deletions

File tree

‎lib/matplotlib/tests/test_ticker.py‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from matplotlib.externals import six
55
import nose.tools
6-
from nose.tools import assert_raises
6+
from nose.tools import assert_equal, assert_raises
77
from numpy.testing import assert_almost_equal
88
import numpy as np
99
import matplotlib
@@ -159,6 +159,54 @@ def test_SymmetricalLogLocator_set_params():
159159
nose.tools.assert_equal(sym.numticks, 8)
160160

161161

162+
@cleanup
163+
def test_ScalarFormatter_offset_value():
164+
fig, ax = plt.subplots()
165+
formatter = ax.get_xaxis().get_major_formatter()
166+
update_ticks = lambda ax: next(ax.get_xaxis().iter_ticks())
167+
168+
ax.set_xlim(123, 189)
169+
update_ticks(ax)
170+
assert_equal(formatter.offset, 0)
171+
172+
ax.set_xlim(-189, -123)
173+
update_ticks(ax)
174+
assert_equal(formatter.offset, 0)
175+
176+
ax.set_xlim(12341, 12349)
177+
update_ticks(ax)
178+
assert_equal(formatter.offset, 12340)
179+
180+
ax.set_xlim(-12349, -12341)
181+
update_ticks(ax)
182+
assert_equal(formatter.offset, -12340)
183+
184+
ax.set_xlim(99999.5, 100010.5)
185+
update_ticks(ax)
186+
assert_equal(formatter.offset, 100000)
187+
188+
ax.set_xlim(-100010.5, -99999.5)
189+
update_ticks(ax)
190+
assert_equal(formatter.offset, -100000)
191+
192+
ax.set_xlim(99990.5, 100000.5)
193+
update_ticks(ax)
194+
assert_equal(formatter.offset, 100000)
195+
196+
ax.set_xlim(-100000.5, -99990.5)
197+
update_ticks(ax)
198+
assert_equal(formatter.offset, -100000)
199+
200+
ax.set_xlim(1233999, 1234001)
201+
update_ticks(ax)
202+
assert_equal(formatter.offset, 1234000)
203+
204+
ax.set_xlim(-1234001, -1233999)
205+
update_ticks(ax)
206+
assert_equal(formatter.offset, -1234000)
207+
208+
209+
162210
def _logfe_helper(formatter, base, locs, i, expected_result):
163211
vals = base**locs
164212
labels = [formatter(x, pos) for (x, pos) in zip(vals, i)]

‎lib/matplotlib/ticker.py‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -548,33 +548,49 @@ def set_locs(self, locs):
548548
vmin, vmax = self.axis.get_view_interval()
549549
d = abs(vmax - vmin)
550550
if self._useOffset:
551-
self._set_offset(d)
551+
self._compute_offset()
552552
self._set_orderOfMagnitude(d)
553553
self._set_format(vmin, vmax)
554554

555-
def _set_offset(self, range):
556-
# offset of 20,001 is 20,000, for example
555+
def _compute_offset(self):
557556
locs = self.locs
558-
559-
if locs is None or not len(locs) or range == 0:
557+
if locs is None or not len(locs):
560558
self.offset = 0
561559
return
560+
# Restrict to visible ticks.
562561
vmin, vmax = sorted(self.axis.get_view_interval())
563562
locs = np.asarray(locs)
564563
locs = locs[(vmin <= locs) & (locs <= vmax)]
565-
ave_loc = np.mean(locs)
566-
if ave_loc: # dont want to take log10(0)
567-
ave_oom = math.floor(math.log10(np.mean(np.absolute(locs))))
568-
range_oom = math.floor(math.log10(range))
569-
570-
if np.absolute(ave_oom - range_oom) >= 3: # four sig-figs
571-
p10 = 10 ** range_oom
572-
if ave_loc < 0:
573-
self.offset = (math.ceil(np.max(locs) / p10) * p10)
574-
else:
575-
self.offset = (math.floor(np.min(locs) / p10) * p10)
576-
else:
577-
self.offset = 0
564+
lmin, lmax = locs.min(), locs.max()
565+
# min, max comparing absolute values (we want division to round towards
566+
# zero so we work on absolute values).
567+
abs_min, abs_max = sorted(map(abs, [lmin, lmax]))
568+
# Only use offset if every tick has the same sign, and if the span is
569+
# small compared to the absolute values.
570+
if lmin <= 0 <= lmax or (abs_max - abs_min) / abs_max >= 1e-2:
571+
self.offset = 0
572+
return
573+
sign = math.copysign(1, lmin)
574+
# What is the smallest power of ten such that abs_min and abs_max are
575+
# equal up to that precision?
576+
oom = 10 ** int(math.log10(abs_max) + 1)
577+
while True:
578+
if abs_min // oom != abs_max // oom:
579+
oom *= 10
580+
break
581+
oom /= 10
582+
if (abs_max - abs_min) / oom <= 1e-2:
583+
# Handle the case of straddling a multiple of a large power of ten
584+
# (relative to the span).
585+
# What is the smallest power of ten such that abs_min and abs_max
586+
# at most 1 apart?
587+
oom = 10 ** int(math.log10(abs_max) + 1)
588+
while True:
589+
if abs_max // oom - abs_min // oom > 1:
590+
oom *= 10
591+
break
592+
oom /= 10
593+
self.offset = sign * (abs_max // oom) * oom
578594

579595
def _set_orderOfMagnitude(self, range):
580596
# if scientific notation is to be used, find the appropriate exponent

0 commit comments

Comments
 (0)