Skip to content

ENH: EngFormatter new kwarg 'sep' #6542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5a550f2
ENH: Add the space_sep option to EngFormatter, and docstring updates
afvincent Jun 6, 2016
722f5d6
FIX: force format_eng(-0.0) to be consistent with format_eng(0)
afvincent Jun 6, 2016
3c221ff
Fix docstrings and comments (remove leading spaces)
afvincent Jun 7, 2016
122cfa2
Simplify and move cleaning strip op. from 'format_eng' to '__call__'
afvincent Jun 7, 2016
07f022d
DOC: update the api example engineering_formatter.py
afvincent Jun 19, 2016
e371e5a
More extensive testing of EngFormatter (including 'space_sep' param)
afvincent Feb 11, 2017
3049a2d
space_sep=bool <- sep=string, and adapt tests
afvincent Feb 12, 2017
e0a2ec8
remove unnecessary trailing 'u' characters in strings
afvincent Feb 12, 2017
1621a2d
fix EngFormatter docstring: escape Unicode character codes
afvincent Feb 12, 2017
c82dcff
'fix' docstring: unindent and reorder bullet list + mention regular t…
afvincent Feb 12, 2017
f09a1b4
Update the example
afvincent Feb 12, 2017
dc9409b
fix test docstring PEP8 errors in test_ticker.py
afvincent Feb 12, 2017
0195beb
Add a 'whats_new' entry
afvincent Feb 12, 2017
3ec1dbd
docstring overhaul: fix bullet list and merge duplicated infos betwee…
afvincent Feb 13, 2017
cd2ac88
use named entities instead of raw unicode codes
afvincent Aug 17, 2017
7f1422c
stop mixing C-style and format-based formatting
afvincent Aug 17, 2017
aba7a7f
Small example tweaking to avoid label cluttering
afvincent Aug 17, 2017
b75f20d
fix an issue with {:g} and str format
afvincent Aug 17, 2017
41a69da
get rid of decimal.Decimal + fix rounding special cases like 999.9...
afvincent Aug 19, 2017
556ec20
Fix the related rounding discrepancies in test_ticker
afvincent Aug 19, 2017
a9af431
(try) fix(ing) an exception about raising an int to a negative power
afvincent Aug 19, 2017
0bde03a
fix some anntzer's comments
afvincent Aug 20, 2017
c98ba91
more complete handling of corner-case roundings + add proper tests
afvincent Aug 20, 2017
60b95ab
Fix some additional remarks made by anntzer
afvincent Aug 21, 2017
ac42b94
Deprecate passing a string as *num* argument
afvincent Aug 24, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more complete handling of corner-case roundings + add proper tests
  • Loading branch information
afvincent committed Aug 25, 2017
commit c98ba91dbe01e08cbfcab5b84c06b27096e8e31e
6 changes: 4 additions & 2 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,13 @@ class TestEngFormatter(object):
(0.1, ('100 m', '100 m', '100.00 m')),
(1, ('1', '1', '1.00')),
(1.23456789, ('1.23457', '1', '1.23')),
(999.9, ('999.9', '1 k', '999.90')),
(999.9, ('999.9', '1 k', '999.90')), # places=0: corner-case rounding
(999.9999, ('1 k', '1 k', '1.00 k')), # corner-case roudning for all
(1000, ('1 k', '1 k', '1.00 k')),
(1001, ('1.001 k', '1 k', '1.00 k')),
(100001, ('100.001 k', '100 k', '100.00 k')),
(987654.321, ('987.654 k', '988 k', '987.65 k'))
(987654.321, ('987.654 k', '988 k', '987.65 k')),
(1.23e27, ('1230 Y', '1230 Y', '1230.00 Y')) # OoR value (> 1000 Y)
]

@pytest.mark.parametrize('input, expected', raw_format_data)
Expand Down
12 changes: 7 additions & 5 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,7 @@ def format_eng(self, num):
"""
dnum = float(num)
sign = 1
fmt = "g" if self.places is None else ".{:d}f".format(self.places)

if dnum < 0:
sign = -1
Expand All @@ -1283,17 +1284,18 @@ def format_eng(self, num):

mant = sign * dnum / (10.0 ** pow10)
# Taking care of the cases like 999.9..., which
# may be rounded to 1000 instead of 1 k.
if (self.places is not None and
round(mant, self.places) >= 1000):
# may be rounded to 1000 instead of 1 k. Beware
# of the corner case of values that are beyond
# the range of SI prefixes (i.e. > 'Y').
_fmant = float("{mant:{fmt}}".format(mant=mant, fmt=fmt))
if _fmant >= 1000 and pow10 != max(self.ENG_PREFIXES):
mant /= 1000
pow10 += 3

prefix = self.ENG_PREFIXES[int(pow10)]

formatted = "{mant:{fmt}}{sep}{prefix}".format(
mant=mant, sep=self.sep, prefix=prefix,
fmt="g" if self.places is None else ".{:d}f".format(self.places))
mant=mant, sep=self.sep, prefix=prefix, fmt=fmt)

return formatted

Expand Down