8000 docstring overhaul: fix bullet list and merge duplicated infos betwee… · matplotlib/matplotlib@b9065f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit b9065f2

Browse files
committed
docstring overhaul: fix bullet list and merge duplicated infos between class and init docstrings
1 parent 06161b5 commit b9065f2

File tree

3 files changed

+36
-38
lines changed

3 files changed

+36
-38
lines changed

examples/api/engineering_formatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# Create artificial data to plot.
1818
# The x data span over several decades to demonstrate several SI prefixes.
1919
xs = np.logspace(1, 9, 100)
20-
ys = (0.8 + 0.4*prng.uniform(size=100))*np.log10(xs)**2
20+
ys = (0.8 + 0.4 * prng.uniform(size=100)) * np.log10(xs)**2
2121

2222
# Figure width is doubled (2*6.4) to display nicely 2 subplots side by side.
2323
fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(12.8, 4.8))

lib/matplotlib/tests/test_ticker.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -576,18 +576,19 @@ def test_params(self, input, expected):
576576
"""
577577
Test the formatting of EngFormatter for various values of the 'places'
578578
argument, in several cases:
579-
0. without a unit symbol but with a (default) space separator;
580-
1. with both a unit symbol and a (default) space separator;
581-
2x. with both a unit symbol and some non default separators;
582-
3x. without a unit symbol but with some non default separators.
579+
0. without a unit symbol but with a (default) space separator;
580+
1. with both a unit symbol and a (default) space separator;
581+
2. with both a unit symbol and some non default separators;
582+
3. without a unit symbol but with some non default separators.
583+
Note that cases 2. and 3. are looped over several separator strings.
583584
"""
584585

585586
UNIT = 's' # seconds
586587
DIGITS = '0123456789' # %timeit showed 10-20% faster search than set
587588

588589
# Case 0: unit='' (default) and sep=' ' (default).
589590
# 'expected' already corresponds to this reference case.
590-
exp_outputs = (_s for _s in expected) # simple copy of 'expected'
591+
exp_outputs = expected
591592
formatters = (
592593
mticker.EngFormatter(), # places=None (default)
593594
mticker.EngFormatter(places=0),
@@ -612,8 +613,9 @@ def test_params(self, input, expected):
612613
# Test several non default separators: no separator, a narrow
613614
# no-break space (unicode character) and an extravagant string.
614615
for _sep in ("", "\u202f", "@_@"):
615-
# Case 2x: unit=UNIT and sep=_sep.
616-
# Remove the space separator from the reference case.
616+
# Case 2: unit=UNIT and sep=_sep.
617+
# Replace the default space separator from the reference case
618+
# with the tested one `_sep` and append a unit symbol to it.
617619
exp_outputs = (_s + _sep + UNIT if _s[-1] in DIGITS # no prefix
618620
else _s.replace(" ", _sep) + UNIT
619621
for _s in expected)
@@ -625,9 +627,9 @@ def test_params(self, input, expected):
625627
for _formatter, _exp_output in zip(formatters, exp_outputs):
626628
assert _formatter(input) == _exp_output
627629

628-
# Case 3x: unit='' (default) and sep=_sep.
629-
# Remove the space separator from the reference case and append
630-
# a unit symbol to it.
630+
# Case 3: unit='' (default) and sep=_sep.
631+
# Replace the default space separator from the reference case
632+
# with the tested one `_sep`. Reference case is already unitless.
631633
exp_outputs = (_s.replace(" ", _sep) for _s in expected)
632634
formatters = (
633635
mticker.EngFormatter(sep=_sep), # places=None (default)

lib/matplotlib/ticker.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,19 +1184,8 @@ class EngFormatter(Formatter):
11841184
"""
11851185
Formats axis values using engineering prefixes to represent powers
11861186
of 1000, plus a specified unit, e.g., 10 MHz instead of 1e7.
1187-
1188-
`unit` is a string containing the abbreviated name of the unit,
1189-
suitable for use with single-letter representations of powers of
1190-
1000. For example, 'Hz' or 'm'.
1191-
1192-
`places` is the precision with which to display the number,
1193-
specified in digits after the decimal point (there will be between
1194-
one and three digits before the decimal point).
1195-
1196-
`sep` is the separator (a string) that is used between the number
1197-
and the prefix/unit. For example, '3.14 mV' if `sep` is " " (default)
1198-
and '3.14mV' if `sep` is "".
11991187
"""
1188+
12001189
# The SI engineering prefixes
12011190
ENG_PREFIXES = {
12021191
-24: "y",
@@ -1222,20 +1211,26 @@ def __init__(self, unit="", places=None, sep=" "):
12221211
"""
12231212
Parameters
12241213
----------
1225-
unit: string (default: "")
1226-
Unit symbol to use.
1227-
1228-
places: int (default: None)
1229-
Precision, i.e. number of digits after the decimal point.
1230-
If it is None, falls back to the floating point format '%g'.
1231-
1232-
sep: string (default: " ")
1233-
6D40 String used between the value and the prefix/unit. Beside the
1234-
default behavior, some other useful use cases may be:
1235-
* sep="" to append directly the prefix/unit to the value;
1236-
* sep="\\u2009" to use a thin space;
1237-
* sep="\\u202f" to use a narrow no-break space;
1238-
* sep="\\u00a0" to use a no-break space.
1214+
unit : str (default: "")
1215+
Unit symbol to use, suitable for use with single-letter
1216+
representations of powers of 1000. For example, 'Hz' or 'm'.
1217+
1218+
places : int (default: None)
1219+
Precision with which to display the number, specified in
1220+
digits after the decimal point (there will be between one
1221+
and three digits before the decimal point). If it is None,
1222+
falls back to the floating point format '%g'.
1223+
1224+
sep : str (default: " ")
1225+
Separator used between the value and the prefix/unit. For
1226+
example, one get '3.14 mV' if ``sep`` is " " (default) and
1227+
'3.14mV' if ``sep`` is "". Besides the default behavior, some
1228+
other useful options may be:
1229+
1230+
* ``sep=""`` to append directly the prefix/unit to the value;
1231+
* ``sep="\\u2009"`` to use a thin space;
1232+
* ``sep="\\u202f"`` to use a narrow no-break space;
1233+
* ``sep="\\u00a0"`` to use a no-break space.
12391234
"""
12401235
self.unit = unit
12411236
self.places = places
@@ -1244,7 +1239,8 @@ def __init__(self, unit="", places=None, sep=" "):
12441239
def __call__(self, x, pos=None):
12451240
s = "%s%s" % (self.format_eng(x), self.unit)
12461241
# Remove the trailing separator when there is neither prefix nor unit
1247-
s = s.strip(self.sep)
1242+
if len(self.sep) > 0 and s.endswith(self.sep):
1243+
s = s[:-len(self.sep)]
12481244
return self.fix_minus(s)
12491245

12501246
def format_eng(self, num):

0 commit comments

Comments
 (0)
0