8000 MAINT: Response to review comments to #6251 · matplotlib/matplotlib@5161407 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5161407

Browse files
committed
MAINT: Response to review comments to #6251
1 parent 5f414a3 commit 5161407

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ def test_formatstrformatter():
367367
nose.tools.assert_equal('00002', tmp_form(2))
368368

369369

370-
def _percent_format_helper(mx, decimals, symbol, x, d, expected):
371-
form = mticker.PercentFormatter(mx, decimals, symbol)
372-
nose.tools.assert_equal(form.format_pct(x, d), expected)
370+
def _percent_format_helper(max, decimals, symbol, x, range, expected):
371+
formatter = mticker.PercentFormatter(max, decimals, symbol)
372+
nose.tools.assert_equal(formatter.format_pct(x, range), expected)
373373

374374

375375
def test_percentformatter():
@@ -395,8 +395,8 @@ def test_percentformatter():
395395
(75, 3, '', 50, 100, '66.667'),
396396
(42, None, '^^Foobar$$', 21, 12, '50.0^^Foobar$$'),
397397
)
398-
for mx, decimals, symbol, x, d, expected in test_cases:
399-
yield _percent_format_helper, mx, decimals, symbol, x, d, expected
398+
for max, decimals, symbol, x, range, expected in test_cases:
399+
yield _percent_format_helper, max, decimals, symbol, x, range, expected
400400

401401

402402
if __name__ == '__main__':

lib/matplotlib/ticker.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,46 +1012,47 @@ def __call__(self, x, pos=None):
10121012
Formats the tick as a percentage with the appropriate scaling.
10131013
"""
10141014
xmin, xmax = self.axis.get_view_interval()
1015-
d = abs(xmax - xmin)
1015+
range = abs(xmax - xmin)
10161016

1017-
return self.fix_minus(self.format_pct(x, d))
1017+
return self.fix_minus(self.format_pct(x, range))
10181018

1019-
def format_pct(self, x, d):
1019+
def format_pct(self, x, range):
10201020
"""
10211021
Formats the number as a percentage number with the correct
10221022
number of decimals and adds the percent symbol, if any.
10231023
10241024
If `self.decimals` is `None`, the number of digits after the
1025-
decimal point is set based on the width of the domain `d` as
1026-
follows:
1027-
1028-
+-------+----------+------------------------+
1029-
| d | decimals | sample |
1030-
+-------+----------+------------------------+
1031-
| >50 | 0 | ``x = 34.5`` => 34% |
1032-
+-------+----------+------------------------+
1033-
| >5 | 1 | ``x = 34.5`` => 34.5% |
1034-
+-------+----------+------------------------+
1035-
| >0.5 | 2 | ``x = 34.5`` => 34.50% |
1036-
+-------+----------+------------------------+
1037-
| ... | ... | ... |
1038-
+-------+----------+------------------------+
1039-
1040-
This method will not be very good for tiny ranges or extremely
1041-
large ranges. It assumes that the values on the chart are
1042-
percentages displayed on a reasonable scale.
1025+
decimal point is set based on the displayed `range` of the axis
1026+
as follows:
1027+
1028+
+--------+----------+------------------------+
1029+
| domain | decimals | sample |
1030+
+--------+----------+------------------------+
1031+
| >50 | 0 | ``x = 34.5`` => 35% |
1032+
+--------+----------+------------------------+
1033+
| >5 | 1 | ``x = 34.5`` => 34.5% |
1034+
+--------+----------+------------------------+
1035+
| >0.5 | 2 | ``x = 34.5`` => 34.50% |
1036+
+--------+----------+------------------------+
1037+
| ... | ... | ... |
1038+
+--------+----------+------------------------+
1039+
1040+
This method will not be very good for tiny axis ranges or
1041+
extremely large ones. It assumes that the values on the chart
1042+
are percentages displayed on a reasonable scale.
10431043
"""
10441044
x = self.convert_to_pct(x)
10451045
if self.decimals is None:
1046-
d = self.convert_to_pct(d) # d is a difference, so this works
1047-
if d <= 0:
1046+
# conversion works because range is a difference
1047+
scaled_range = self.convert_to_pct(range)
1048+
if scaled_range <= 0:
10481049
decimals = 0
10491050
else:
10501051
# Luckily Python's built-in `ceil` rounds to +inf, not away
10511052
# from zero. This is very important since the equation for
1052-
# `decimals` starts out as `d > 0.5 * 10**(2 - decimals)`
1053-
# and ends up with `decimals > 2 - log10(2 * d)`.
1054-
decimals = math.ceil(2.0 - math.log10(2.0 * d))
1053+
# `decimals` starts out as `scaled_range > 0.5 * 10**(2 - decimals)`
1054+
# and ends up with `decimals > 2 - log10(2 * scaled_range)`.
1055+
decimals = math.ceil(2.0 - math.log10(2.0 * scaled_ranged))
10551056
if decimals > 5:
10561057
decimals = 5
10571058
elif decimals < 0:

0 commit comments

Comments
 (0)
0