Skip to content

Commit

Permalink
MAINT: Response to review comments to #6251
Browse files Browse the repository at this point in the history
  • Loading branch information
madphysicist committed Apr 8, 2016
1 parent 5f414a3 commit 5161407
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
10 changes: 5 additions & 5 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ def test_formatstrformatter():
nose.tools.assert_equal('00002', tmp_form(2))


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


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


if __name__ == '__main__':
Expand Down
53 changes: 27 additions & 26 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,46 +1012,47 @@ def __call__(self, x, pos=None):
Formats the tick as a percentage with the appropriate scaling.
"""
xmin, xmax = self.axis.get_view_interval()
d = abs(xmax - xmin)
range = abs(xmax - xmin)

return self.fix_minus(self.format_pct(x, d))
return self.fix_minus(self.format_pct(x, range))

def format_pct(self, x, d):
def format_pct(self, x, range):
"""
Formats the number as a percentage number with the correct
number of decimals and adds the percent symbol, if any.
If `self.decimals` is `None`, the number of digits after the
decimal point is set based on the width of the domain `d` as
follows:
+-------+----------+------------------------+
| d | decimals | sample |
+-------+----------+------------------------+
| >50 | 0 | ``x = 34.5`` => 34% |
+-------+----------+------------------------+
| >5 | 1 | ``x = 34.5`` => 34.5% |
+-------+----------+------------------------+
| >0.5 | 2 | ``x = 34.5`` => 34.50% |
+-------+----------+------------------------+
| ... | ... | ... |
+-------+----------+------------------------+
This method will not be very good for tiny ranges or extremely
large ranges. It assumes that the values on the chart are
percentages displayed on a reasonable scale.
decimal point is set based on the displayed `range` of the axis
as follows:
+--------+----------+------------------------+
| domain | decimals | sample |
+--------+----------+------------------------+
| >50 | 0 | ``x = 34.5`` => 35% |
+--------+----------+------------------------+
| >5 | 1 | ``x = 34.5`` => 34.5% |
+--------+----------+------------------------+
| >0.5 | 2 | ``x = 34.5`` => 34.50% |
+--------+----------+------------------------+
| ... | ... | ... |
+--------+----------+------------------------+
This method will not be very good for tiny axis ranges or
extremely large ones. It assumes that the values on the chart
are percentages displayed on a reasonable scale.
"""
x = self.convert_to_pct(x)
if self.decimals is None:
d = self.convert_to_pct(d) # d is a difference, so this works
if d <= 0:
# conversion works because range is a difference
scaled_range = self.convert_to_pct(range)
if scaled_range <= 0:
decimals = 0
else:
# Luckily Python's built-in `ceil` rounds to +inf, not away
# from zero. This is very important since the equation for
# `decimals` starts out as `d > 0.5 * 10**(2 - decimals)`
# and ends up with `decimals > 2 - log10(2 * d)`.
decimals = math.ceil(2.0 - math.log10(2.0 * d))
# `decimals` starts out as `scaled_range > 0.5 * 10**(2 - decimals)`
# and ends up with `decimals > 2 - log10(2 * scaled_range)`.
decimals = math.ceil(2.0 - math.log10(2.0 * scaled_ranged))
if decimals > 5:
decimals = 5
elif decimals < 0:
Expand Down

0 comments on commit 5161407

Please sign in to comment.