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

Skip to content

Commit 93cfbcf

Browse files
committed
ticker: Improve and rename EngFormatter offset test
1 parent 1074bb8 commit 93cfbcf

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,19 +1594,47 @@ def test_engformatter_usetex_useMathText():
15941594
assert x_tick_label_text == ['$0$', '$500$', '$1$ k']
15951595

15961596

1597-
def test_engformatter_useOffset():
1597+
@pytest.mark.parametrize(
1598+
'oom_center, oom_noise', [
1599+
(15, 6),
1600+
(12, 3),
1601+
(9, 3),
1602+
(9, 6),
1603+
(0, -6),
1604+
# Going to lower orders of magnitudes will make this test fail due to
1605+
# floating points approximated as zero. Putting 12 and above oom difference
1606+
# between center and the noise will also fail, due to ticks give different
1607+
# unit prefixes
1608+
]
1609+
)
1610+
def test_engformatter_offset(oom_center, oom_noise):
1611+
UNIT = "eV"
1612+
# Intentionally picking also negative numbers
1613+
r = range(-2, 9)
15981614
fig, ax = plt.subplots()
1599-
data_offset = 10000000
1600-
ydata = range(data_offset, data_offset+5)
1615+
data_offset = 10**oom_center
1616+
ydata = data_offset + np.array(r, dtype=float)*10**oom_noise
16011617
ax.plot(ydata)
16021618
ax.set_yticks(ydata)
1603-
ax.yaxis.set_major_formatter(mticker.EngFormatter(useOffset=True, unit="Hz"))
1619+
formatter = mticker.EngFormatter(useOffset=True, unit=UNIT)
1620+
ax.yaxis.set_major_formatter(formatter)
16041621
fig.canvas.draw()
1605-
y_tick_label_text = [labl.get_text() for labl in ax.get_yticklabels()]
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
1622+
ticksGot = [labl.get_text() for labl in ax.get_yticklabels()]
1623+
ticksExpected = list(map(
1624+
# Fix negative number's - sign in case unicode is used
1625+
lambda n: formatter.fix_minus(str(n)) + " " + (
1626+
# zero is zero, no matter what is the oom
1627+
formatter.ENG_PREFIXES[oom_noise] if n != 0 else ""
1628+
) + UNIT,
1629+
r
1630+
))
1631+
offsetGot = ax.yaxis.get_major_formatter().get_offset()
1632+
offsetExpected = "+1 {}{}".format(
1633+
formatter.ENG_PREFIXES[oom_center],
1634+
UNIT
1635+
)
1636+
assert ticksGot == ticksExpected
1637+
assert offsetExpected == offsetGot
16101638

16111639

16121640
class TestPercentFormatter:

0 commit comments

Comments
 (0)