diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 94f66e8060e7..b61345d37086 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -450,6 +450,26 @@ def test_percentformatter(): yield (_percent_format_helper,) + case +def test_EngFormatter_formatting(): + """ + Create two instances of EngFormatter with default parameters, with and + without a unit string ('s' for seconds). Test the formatting in some cases, + especially the case when no SI prefix is present, for values in [1, 1000). + + Should not raise exceptions. + """ + unitless = mticker.EngFormatter() + nose.tools.assert_equal(unitless(0.1), u'100 m') + nose.tools.assert_equal(unitless(1), u'1') + nose.tools.assert_equal(unitless(999.9), u'999.9') + nose.tools.assert_equal(unitless(1001), u'1.001 k') + + with_unit = mticker.EngFormatter(unit=u's') + nose.tools.assert_equal(with_unit(0.1), u'100 ms') + nose.tools.assert_equal(with_unit(1), u'1 s') + nose.tools.assert_equal(with_unit(999.9), u'999.9 s') + nose.tools.assert_equal(with_unit(1001), u'1.001 ks') + if __name__ == '__main__': import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 782187cd09d1..b878589ed051 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -1033,7 +1033,7 @@ class EngFormatter(Formatter): suitable for use with single-letter representations of powers of 1000. For example, 'Hz' or 'm'. - `places` is the percision with which to display the number, + `places` is the precision with which to display the number, specified in digits after the decimal point (there will be between one and three digits before the decimal point). """ @@ -1113,7 +1113,11 @@ def format_eng(self, num): formatted = format_str % (mant, prefix) - return formatted.strip() + formatted = formatted.strip() + if (self.unit != "") and (prefix == self.ENG_PREFIXES[0]): + formatted = formatted + " " + + return formatted class PercentFormatter(Formatter):