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

Skip to content

Commit 1074bb8

Browse files
committed
ticker.EngFormatter: Simplify by always using self.orderOfMagnitude = 0
1 parent a2c766b commit 1074bb8

File tree

3 files changed

+25
-40
lines changed

3 files changed

+25
-40
lines changed

doc/users/next_whats_new/EngFormatter-offset.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ Here's an example result:
1818
import matplotlib.ticker as mticker
1919

2020
fig, ax = plt.subplots()
21-
offset = int(1e7)
22-
ydata = range(offset, offset+5)
21+
data_offset = 10000000
22+
ydata = range(data_offset, data_offset+5)
2323
ax.plot(ydata)
24-
ax.set_yticks(ydata)
2524
ax.yaxis.set_major_formatter(mticker.EngFormatter(useOffset=True, unit="Hz"))
2625
plt.show()

lib/matplotlib/tests/test_ticker.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,14 +1596,17 @@ def test_engformatter_usetex_useMathText():
15961596

15971597
def test_engformatter_useOffset():
15981598
fig, ax = plt.subplots()
1599-
offset = int(1e7)
1600-
ydata = range(offset, offset+5)
1599+
data_offset = 10000000
1600+
ydata = range(data_offset, data_offset+5)
16011601
ax.plot(ydata)
16021602
ax.set_yticks(ydata)
16031603
ax.yaxis.set_major_formatter(mticker.EngFormatter(useOffset=True, unit="Hz"))
16041604
fig.canvas.draw()
16051605
y_tick_label_text = [labl.get_text() for labl in ax.get_yticklabels()]
1606-
assert y_tick_label_text == (np.array(ydata)-offset).astype(str).tolist()
1606+
offset = ax.yaxis.get_major_formatter().get_offset()
1607+
expectedTicks = list(map("{} Hz".format, np.array(ydata) - data_offset))
1608+
assert y_tick_label_text == expectedTicks
1609+
assert "+10 MHz" == offset
16071610

16081611

16091612
class TestPercentFormatter:

lib/matplotlib/ticker.py

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,13 +1432,16 @@ def __call__(self, x, pos=None):
14321432
if len(self.locs) == 0 or self.offset == 0:
14331433
return self.fix_minus(self.format_data(x))
14341434
else:
1435-
xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
1435+
# Here in ScalarFormatter.__call__ we also check
1436+
# self.orderOfMagnitude. In this class, it is always 0, since the
1437+
# oom of the data is represented by the ENG_PREFIXES prepending
1438+
# the ticks.
1439+
xp = x - self.offset
14361440
if abs(xp) < 1e-8:
14371441
xp = 0
1438-
# The ScalarFormatter.__call__ reads the locale here as well. We
1439-
# don't care about it, and we want the ticks to also get the unit
1440-
# with the best prefix as computed by self.format_data that is
1441-
# produced by it doesn't.
1442+
# Also in contrary to ScalarFormatter.__call__ we use the
1443+
# conditioned tex formatting provided by self.format_data and we
1444+
# don't care about the locale.
14421445
return self.fix_minus(self.format_data(xp))
14431446

14441447
def set_locs(self, locs):
@@ -1447,41 +1450,21 @@ def set_locs(self, locs):
14471450
if len(self.locs) > 0:
14481451
if self._useOffset:
14491452
self._compute_offset()
1450-
self._set_order_of_magnitude()
1451-
# This is what's different from ScalarFormatter: We search among
1452-
# the engineers' standard orders of magnitudes (0, -3, 3, -6, 6,
1453-
# -9, 9 etc) the oom closest to our self.orderOfMagnitude. Then we
1454-
# set our self.orderOfMagnitude to it.
1455-
c = abs(self.orderOfMagnitude)
1456-
for sciOom in itertools.count(0, 3):
1457-
if c <= sciOom:
1458-
self.orderOfMagnitude = math.copysign(sciOom, self.orderOfMagnitude)
1459-
break
1460-
self._set_format()
14611453

14621454
# Simplify a bit ScalarFormatter.get_offset: We always want to use
1463-
# self.format_data. We insert here the surrounding $...$ here, if tex /
1464-
# mathtext is set.
1455+
# self.format_data, and we don't need to handle tex related features as
1456+
# these are handled well by self.format_data. Also we don't need to check
1457+
# self.orderOfMagnitude since we want it to always be 0, because the ticks
1458+
# themselves represetnt the order of magnitude of the data much better.
14651459
def get_offset(self):
14661460
# docstring inherited
14671461
if len(self.locs) == 0:
14681462
return ''
1469-
if self.orderOfMagnitude or self.offset:
1470-
offsetStr = ''
1471-
sciNotStr = ''
1472-
if self.offset:
1473-
offsetStr = self.format_data(self.offset)
1474-
if self.offset > 0:
1475-
offsetStr = '+' + offsetStr
1476-
if self.orderOfMagnitude:
1477-
sciNotStr = self.format_data(10 ** self.orderOfMagnitude)
1478-
if self._useMathText or self._usetex:
1479-
if sciNotStr != '':
1480-
sciNotStr = r'\times%s' % sciNotStr
1481-
s = fr'${sciNotStr}{offsetStr}$'
1482-
else:
1483-
s = ''.join((sciNotStr, offsetStr))
1484-
return self.fix_minus(s)
1463+
if self.offset:
1464+
offsetStr = self.format_data(self.offset)
1465+
if self.offset > 0:
1466+
offsetStr = '+' + offsetStr
1467+
return self.fix_minus(offsetStr)
14851468
return ''
14861469

14871470
def format_eng(self, num):

0 commit comments

Comments
 (0)