diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index b564fb6df1b1..d89d6119c0bb 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -882,6 +882,13 @@ def test_LogFormatter_call(self, val): temp_lf.axis = FakeAxis() assert temp_lf(val) == str(val) + @pytest.mark.parametrize('val', [1e-323, 2e-323, 10e-323, 11e-323]) + def test_LogFormatter_call_tiny(self, val): + # test coeff computation in __call__ + temp_lf = mticker.LogFormatter() + temp_lf.axis = FakeAxis() + temp_lf(val) + class TestLogitFormatter: @staticmethod diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 7a5e3ea60501..21097e282c73 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -1070,7 +1070,7 @@ def __call__(self, x, pos=None): fx = math.log(x) / math.log(b) is_x_decade = is_close_to_int(fx) exponent = round(fx) if is_x_decade else np.floor(fx) - coeff = round(x / b ** exponent) + coeff = round(b ** (fx - exponent)) if self.labelOnlyBase and not is_x_decade: return '' @@ -1154,7 +1154,7 @@ def __call__(self, x, pos=None): fx = math.log(x) / math.log(b) is_x_decade = is_close_to_int(fx) exponent = round(fx) if is_x_decade else np.floor(fx) - coeff = round(x / b ** exponent) + coeff = round(b ** (fx - exponent)) if is_x_decade: fx = round(fx) @@ -1186,7 +1186,7 @@ def _non_decade_format(self, sign_string, base, fx, usetex): """Return string for non-decade locations.""" b = float(base) exponent = math.floor(fx) - coeff = b ** fx / b ** exponent + coeff = b ** (fx - exponent) if is_close_to_int(coeff): coeff = round(coeff) return r'$\mathdefault{%s%g\times%s^{%d}}$' \