Format numbers as a percentage. Parameters ---------- xmax : float Determines how the number is converted into a percentage. *xmax* is the data value that corresponds to 100%. Percentages are computed as ``x / xmax * 100``. So if the data is already
| 1605 | |
| 1606 | |
| 1607 | class PercentFormatter(Formatter): |
| 1608 | """ |
| 1609 | Format numbers as a percentage. |
| 1610 | |
| 1611 | Parameters |
| 1612 | ---------- |
| 1613 | xmax : float |
| 1614 | Determines how the number is converted into a percentage. |
| 1615 | *xmax* is the data value that corresponds to 100%. |
| 1616 | Percentages are computed as ``x / xmax * 100``. So if the data is |
| 1617 | already scaled to be percentages, *xmax* will be 100. Another common |
| 1618 | situation is where *xmax* is 1.0. |
| 1619 | |
| 1620 | decimals : None or int |
| 1621 | The number of decimal places to place after the point. |
| 1622 | If *None* (the default), the number will be computed automatically. |
| 1623 | |
| 1624 | symbol : str or None |
| 1625 | A string that will be appended to the label. It may be |
| 1626 | *None* or empty to indicate that no symbol should be used. LaTeX |
| 1627 | special characters are escaped in *symbol* whenever latex mode is |
| 1628 | enabled, unless *is_latex* is *True*. |
| 1629 | |
| 1630 | is_latex : bool |
| 1631 | If *False*, reserved LaTeX characters in *symbol* will be escaped. |
| 1632 | """ |
| 1633 | def __init__(self, xmax=100, decimals=None, symbol='%', is_latex=False): |
| 1634 | self.xmax = xmax + 0.0 |
| 1635 | self.decimals = decimals |
| 1636 | self._symbol = symbol |
| 1637 | self._is_latex = is_latex |
| 1638 | |
| 1639 | def __call__(self, x, pos=None): |
| 1640 | """Format the tick as a percentage with the appropriate scaling.""" |
| 1641 | ax_min, ax_max = self.axis.get_view_interval() |
| 1642 | display_range = abs(ax_max - ax_min) |
| 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 |
no outgoing calls
no test coverage detected
searching dependent graphs…