Format 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 *display_range* of the axis as follows:
(self, x, display_range)
| 1643 | return self.fix_minus(self.format_pct(x, display_range)) |
| 1644 | |
| 1645 | def format_pct(self, x, display_range): |
| 1646 | """ |
| 1647 | Format the number as a percentage number with the correct |
| 1648 | number of decimals and adds the percent symbol, if any. |
| 1649 | |
| 1650 | If ``self.decimals`` is `None`, the number of digits after the |
| 1651 | decimal point is set based on the *display_range* of the axis |
| 1652 | as follows: |
| 1653 | |
| 1654 | ============= ======== ======================= |
| 1655 | display_range decimals sample |
| 1656 | ============= ======== ======================= |
| 1657 | >50 0 ``x = 34.5`` => 35% |
| 1658 | >5 1 ``x = 34.5`` => 34.5% |
| 1659 | >0.5 2 ``x = 34.5`` => 34.50% |
| 1660 | ... ... ... |
| 1661 | ============= ======== ======================= |
| 1662 | |
| 1663 | This method will not be very good for tiny axis ranges or |
| 1664 | extremely large ones. It assumes that the values on the chart |
| 1665 | are percentages displayed on a reasonable scale. |
| 1666 | """ |
| 1667 | x = self.convert_to_pct(x) |
| 1668 | if self.decimals is None: |
| 1669 | # conversion works because display_range is a difference |
| 1670 | scaled_range = self.convert_to_pct(display_range) |
| 1671 | if scaled_range <= 0: |
| 1672 | decimals = 0 |
| 1673 | else: |
| 1674 | # Luckily Python's built-in ceil rounds to +inf, not away from |
| 1675 | # zero. This is very important since the equation for decimals |
| 1676 | # starts out as `scaled_range > 0.5 * 10**(2 - decimals)` |
| 1677 | # and ends up with `decimals > 2 - log10(2 * scaled_range)`. |
| 1678 | decimals = math.ceil(2.0 - math.log10(2.0 * scaled_range)) |
| 1679 | if decimals > 5: |
| 1680 | decimals = 5 |
| 1681 | elif decimals < 0: |
| 1682 | decimals = 0 |
| 1683 | else: |
| 1684 | decimals = self.decimals |
| 1685 | s = f'{x:0.{int(decimals)}f}' |
| 1686 | |
| 1687 | return s + self.symbol |
| 1688 | |
| 1689 | def convert_to_pct(self, x): |
| 1690 | return 100.0 * (x / self.xmax) |