Skip to content

Commit aed13c1

Browse files
pharshalptimhoffm
authored andcommitted
FIX: make EngFormatter respect axes.unicode_minus rcParam (#13477)
* Add fix_minus method to EngFormatter * Avoid duplicating fix_minus method * Updated tests to reflect the new (correct) behavior * Updated EngFormatter test to include axes.unicode_minus = False * Removed plt.rcdefaults()
1 parent 8137b4c commit aed13c1

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -692,33 +692,48 @@ def test_basic(self, format, input, expected):
692692

693693

694694
class TestEngFormatter(object):
695-
# (input, expected) where ''expected'' corresponds to the outputs
696-
# respectively returned when (places=None, places=0, places=2)
695+
# (unicode_minus, input, expected) where ''expected'' corresponds to the
696+
# outputs respectively returned when (places=None, places=0, places=2)
697+
# unicode_minus is a boolean value for the rcParam['axes.unicode_minus']
697698
raw_format_data = [
698-
(-1234.56789, ('-1.23457 k', '-1 k', '-1.23 k')),
699-
(-1.23456789, ('-1.23457', '-1', '-1.23')),
700-
(-0.123456789, ('-123.457 m', '-123 m', '-123.46 m')),
701-
(-0.00123456789, ('-1.23457 m', '-1 m', '-1.23 m')),
702-
(-0.0, ('0', '0', '0.00')),
703-
(-0, ('0', '0', '0.00')),
704-
(0, ('0', '0', '0.00')),
705-
(1.23456789e-6, ('1.23457 µ', '1 µ', '1.23 µ')),
706-
(0.123456789, ('123.457 m', '123 m', '123.46 m')),
707-
(0.1, ('100 m', '100 m', '100.00 m')),
708-
(1, ('1', '1', '1.00')),
709-
(1.23456789, ('1.23457', '1', '1.23')),
710-
(999.9, ('999.9', '1 k', '999.90')), # places=0: corner-case rounding
711-
(999.9999, ('1 k', '1 k', '1.00 k')), # corner-case rounding for all
712-
(-999.9999, ('-1 k', '-1 k', '-1.00 k')), # negative corner-case
713-
(1000, ('1 k', '1 k', '1.00 k')),
714-
(1001, ('1.001 k', '1 k', '1.00 k')),
715-
(100001, ('100.001 k', '100 k', '100.00 k')),
716-
(987654.321, ('987.654 k', '988 k', '987.65 k')),
717-
(1.23e27, ('1230 Y', '1230 Y', '1230.00 Y')) # OoR value (> 1000 Y)
699+
(False, -1234.56789, ('-1.23457 k', '-1 k', '-1.23 k')),
700+
(True, -1234.56789, ('\N{MINUS SIGN}1.23457 k', '\N{MINUS SIGN}1 k',
701+
'\N{MINUS SIGN}1.23 k')),
702+
(False, -1.23456789, ('-1.23457', '-1', '-1.23')),
703+
(True, -1.23456789, ('\N{MINUS SIGN}1.23457', '\N{MINUS SIGN}1',
704+
'\N{MINUS SIGN}1.23')),
705+
(False, -0.123456789, ('-123.457 m', '-123 m', '-123.46 m')),
706+
(True, -0.123456789, ('\N{MINUS SIGN}123.457 m', '\N{MINUS SIGN}123 m',
707+
'\N{MINUS SIGN}123.46 m')),
708+
(False, -0.00123456789, ('-1.23457 m', '-1 m', '-1.23 m')),
709+
(True, -0.00123456789, ('\N{MINUS SIGN}1.23457 m', '\N{MINUS SIGN}1 m',
710+
'\N{MINUS SIGN}1.23 m')),
711+
(True, -0.0, ('0', '0', '0.00')),
712+
(True, -0, ('0', '0', '0.00')),
713+
(True, 0, ('0', '0', '0.00')),
714+
(True, 1.23456789e-6, ('1.23457 µ', '1 µ', '1.23 µ')),
715+
(True, 0.123456789, ('123.457 m', '123 m', '123.46 m')),
716+
(True, 0.1, ('100 m', '100 m', '100.00 m')),
717+
(True, 1, ('1', '1', '1.00')),
718+
(True, 1.23456789, ('1.23457', '1', '1.23')),
719+
# places=0: corner-case rounding
720+
(True, 999.9, ('999.9', '1 k', '999.90')),
721+
# corner-case rounding for all
722+
(True, 999.9999, ('1 k', '1 k', '1.00 k')),
723+
# negative corner-case
724+
(False, -999.9999, ('-1 k', '-1 k', '-1.00 k')),
725+
(True, -999.9999, ('\N{MINUS SIGN}1 k', '\N{MINUS SIGN}1 k',
726+
'\N{MINUS SIGN}1.00 k')),
727+
(True, 1000, ('1 k', '1 k', '1.00 k')),
728+
(True, 1001, ('1.001 k', '1 k', '1.00 k')),
729+
(True, 100001, ('100.001 k', '100 k', '100.00 k')),
730+
(True, 987654.321, ('987.654 k', '988 k', '987.65 k')),
731+
# OoR value (> 1000 Y)
732+
(True, 1.23e27, ('1230 Y', '1230 Y', '1230.00 Y'))
718733
]
719734

720-
@pytest.mark.parametrize('input, expected', raw_format_data)
721-
def test_params(self, input, expected):
735+
@pytest.mark.parametrize('unicode_minus, input, expected', raw_format_data)
736+
def test_params(self, unicode_minus, input, expected):
722737
"""
723738
Test the formatting of EngFormatter for various values of the 'places'
724739
argument, in several cases:
@@ -729,6 +744,7 @@ def test_params(self, input, expected):
729744
Note that cases 2. and 3. are looped over several separator strings.
730745
"""
731746

747+
plt.rcParams['axes.unicode_minus'] = unicode_minus
732748
UNIT = 's' # seconds
733749
DIGITS = '0123456789' # %timeit showed 10-20% faster search than set
734750

lib/matplotlib/ticker.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,12 @@ def set_useMathText(self, val):
12721272

12731273
useMathText = property(fget=get_useMathText, fset=set_useMathText)
12741274

1275+
def fix_minus(self, s):
1276+
"""
1277+
Replace hyphens with a unicode minus.
1278+
"""
1279+
return ScalarFormatter.fix_minus(self, s)
1280+
12751281
def __call__(self, x, pos=None):
12761282
s = "%s%s" % (self.format_eng(x), self.unit)
12771283
# Remove the trailing separator when there is neither prefix nor unit

0 commit comments

Comments
 (0)