Skip to content

Commit 0ac187d

Browse files
committed
Merge pull request #6014 from afvincent/afvincent-patch-issue-6009
FIX: EngFormatter without but without prefix closes #6009
2 parents 4ba20b8 + 2ab06c5 commit 0ac187d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/matplotlib/tests/test_ticker.py

+20
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,26 @@ def test_percentformatter():
450450
yield (_percent_format_helper,) + case
451451

452452

453+
def test_EngFormatter_formatting():
454+
"""
455+
Create two instances of EngFormatter with default parameters, with and
456+
without a unit string ('s' for seconds). Test the formatting in some cases,
457+
especially the case when no SI prefix is present, for values in [1, 1000).
458+
459+
Should not raise exceptions.
460+
"""
461+
unitless = mticker.EngFormatter()
462+
nose.tools.assert_equal(unitless(0.1), u'100 m')
463+
nose.tools.assert_equal(unitless(1), u'1')
464+
nose.tools.assert_equal(unitless(999.9), u'999.9')
465+
nose.tools.assert_equal(unitless(1001), u'1.001 k')
466+
467+
with_unit = mticker.EngFormatter(unit=u's')
468+
nose.tools.assert_equal(with_unit(0.1), u'100 ms')
469+
nose.tools.assert_equal(with_unit(1), u'1 s')
470+
nose.tools.assert_equal(with_unit(999.9), u'999.9 s')
471+
nose.tools.assert_equal(with_unit(1001), u'1.001 ks')
472+
453473
if __name__ == '__main__':
454474
import nose
455475
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/ticker.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ class EngFormatter(Formatter):
10331033
suitable for use with single-letter representations of powers of
10341034
1000. For example, 'Hz' or 'm'.
10351035
1036-
`places` is the percision with which to display the number,
1036+
`places` is the precision with which to display the number,
10371037
specified in digits after the decimal point (there will be between
10381038
one and three digits before the decimal point).
10391039
"""
@@ -1113,7 +1113,11 @@ def format_eng(self, num):
11131113

11141114
formatted = format_str % (mant, prefix)
11151115

1116-
return formatted.strip()
1116+
formatted = formatted.strip()
1117+
if (self.unit != "") and (prefix == self.ENG_PREFIXES[0]):
1118+
formatted = formatted + " "
1119+
1120+
return formatted
11171121

11181122

11191123
class PercentFormatter(Formatter):

0 commit comments

Comments
 (0)