From d98ebffdf056543a9e2a726f43d9cae5563f512f Mon Sep 17 00:00:00 2001 From: Steffen Rehberg Date: Wed, 29 Dec 2021 16:57:55 +0100 Subject: [PATCH] FIX: Remove trailing zeros from offset significand Fix regression in 3.4.0 introduced by b5fb685a61f0 (#18506). --- lib/matplotlib/tests/test_ticker.py | 13 +++++++++++++ lib/matplotlib/ticker.py | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index d99fd5a306b9..a66e17b8d0f5 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -500,6 +500,13 @@ class TestScalarFormatter: [12.3, "12.300"], ] + format_data = [ + (.1, "1e-1"), + (.11, "1.1e-1"), + (1e8, "1e8"), + (1.1e8, "1.1e8"), + ] + @pytest.mark.parametrize('unicode_minus, result', [(True, "\N{MINUS SIGN}1"), (False, "-1")]) def test_unicode_minus(self, unicode_minus, result): @@ -561,6 +568,12 @@ def test_scilimits(self, sci_type, scilimits, lim, orderOfMag, fewticks): tmp_form.set_locs(ax.yaxis.get_majorticklocs()) assert orderOfMag == tmp_form.orderOfMagnitude + @pytest.mark.parametrize('value, expected', format_data) + def test_format_data(self, value, expected): + mpl.rcParams['axes.unicode_minus'] = False + sf = mticker.ScalarFormatter() + assert sf.format_data(value) == expected + @pytest.mark.parametrize('data, expected', cursor_data) def test_cursor_precision(self, data, expected): fig, ax = plt.subplots() diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 0d52954ac6d9..bd9da7437e84 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -664,7 +664,7 @@ def format_data(self, value): s = round(value / 10**e, 10) exponent = self._format_maybe_minus_and_locale("%d", e) significand = self._format_maybe_minus_and_locale( - "%d" if s % 1 == 0 else "%1.10f", s) + "%d" if s % 1 == 0 else "%1.10g", s) if e == 0: return significand elif self._useMathText or self._usetex: