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

Skip to content

Commit 59e0ab9

Browse files
committed
Merge pull request #6014 from afvincent/afvincent-patch-issue-6009
FIX: EngFormatter without but without prefix closes #6009
1 parent 1e35d8d commit 59e0ab9

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,27 @@ def test_formatstrformatter():
413413
tmp_form = mticker.StrMethodFormatter('{x:05d}')
414414
nose.tools.assert_equal('00002', tmp_form(2))
415415

416+
417+
def test_EngFormatter_formatting():
418+
"""
419+
Create two instances of EngFormatter with default parameters, with and
420+
without a unit string ('s' for seconds). Test the formatting in some cases,
421+
especially the case when no SI prefix is present, for values in [1, 1000).
422+
423+
Should not raise exceptions.
424+
"""
425+
unitless = mticker.EngFormatter()
426+
nose.tools.assert_equal(unitless(0.1), u'100 m')
427+
nose.tools.assert_equal(unitless(1), u'1')
428+
nose.tools.assert_equal(unitless(999.9), u'999.9')
429+
nose.tools.assert_equal(unitless(1001), u'1.001 k')
430+
431+
with_unit = mticker.EngFormatter(unit=u's')
432+
nose.tools.assert_equal(with_unit(0.1), u'100 ms')
433+
nose.tools.assert_equal(with_unit(1), u'1 s')
434+
nose.tools.assert_equal(with_unit(999.9), u'999.9 s')
435+
nose.tools.assert_equal(with_unit(1001), u'1.001 ks')
436+
416437
if __name__ == '__main__':
417438
import nose
418439
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/ticker.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ class EngFormatter(Formatter):
10051005
suitable for use with single-letter representations of powers of
10061006
1000. For example, 'Hz' or 'm'.
10071007
1008-
`places` is the percision with which to display the number,
1008+
`places` is the precision with which to display the number,
10091009
specified in digits after the decimal point (there will be between
10101010
one and three digits before the decimal point).
10111011
"""
@@ -1087,7 +1087,11 @@ def format_eng(self, num):
10871087

10881088
formatted = format_str % (mant, prefix)
10891089

1090-
return formatted.strip()
1090+
formatted = formatted.strip()
1091+
if (self.unit != "") and (prefix == self.ENG_PREFIXES[0]):
1092+
formatted = formatted + " "
1093+
1094+
return formatted
10911095

10921096

10931097
class Locator(TickHelper):

0 commit comments

Comments
 (0)