Helper for `.MaxNLocator`, `.MultipleLocator`, etc. Take floating-point precision limitations into account when calculating tick locations as integer multiples of a step.
| 2061 | |
| 2062 | |
| 2063 | class _Edge_integer: |
| 2064 | """ |
| 2065 | Helper for `.MaxNLocator`, `.MultipleLocator`, etc. |
| 2066 | |
| 2067 | Take floating-point precision limitations into account when calculating |
| 2068 | tick locations as integer multiples of a step. |
| 2069 | """ |
| 2070 | |
| 2071 | def __init__(self, step, offset): |
| 2072 | """ |
| 2073 | Parameters |
| 2074 | ---------- |
| 2075 | step : float > 0 |
| 2076 | Interval between ticks. |
| 2077 | offset : float |
| 2078 | Offset subtracted from the data limits prior to calculating tick |
| 2079 | locations. |
| 2080 | """ |
| 2081 | if step <= 0: |
| 2082 | raise ValueError("'step' must be positive") |
| 2083 | self.step = step |
| 2084 | self._offset = abs(offset) |
| 2085 | |
| 2086 | def closeto(self, ms, edge): |
| 2087 | # Allow more slop when the offset is large compared to the step. |
| 2088 | if self._offset > 0: |
| 2089 | digits = np.log10(self._offset / self.step) |
| 2090 | tol = max(1e-10, 10 ** (digits - 12)) |
| 2091 | tol = min(0.4999, tol) |
| 2092 | else: |
| 2093 | tol = 1e-10 |
| 2094 | return abs(ms - edge) < tol |
| 2095 | |
| 2096 | def le(self, x): |
| 2097 | """Return the largest n: n*step <= x.""" |
| 2098 | d, m = divmod(x, self.step) |
| 2099 | if self.closeto(m / self.step, 1): |
| 2100 | return d + 1 |
| 2101 | return d |
| 2102 | |
| 2103 | def ge(self, x): |
| 2104 | """Return the smallest n: n*step >= x.""" |
| 2105 | d, m = divmod(x, self.step) |
| 2106 | if self.closeto(m / self.step, 0): |
| 2107 | return d |
| 2108 | return d + 1 |
| 2109 | |
| 2110 | |
| 2111 | class MaxNLocator(Locator): |
no outgoing calls
no test coverage detected
searching dependent graphs…