@@ -1594,19 +1594,47 @@ def test_engformatter_usetex_useMathText():
1594
1594
assert x_tick_label_text == ['$0$' , '$500$' , '$1$ k' ]
1595
1595
1596
1596
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 )
1598
1614
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
1601
1617
ax .plot (ydata )
1602
1618
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 )
1604
1621
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
1610
1638
1611
1639
1612
1640
class TestPercentFormatter :
0 commit comments