From b1bcc9818eda7292add4dcbcd1fb8e63879b17fc Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Wed, 30 Oct 2024 17:29:39 +0200 Subject: [PATCH 1/4] Add anologous tests for LogFormatter_call_big --- lib/matplotlib/tests/test_ticker.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 222a0d7e11b0..95c61167aba8 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1259,6 +1259,13 @@ def test_LogFormatter_call_tiny(self, val): temp_lf.axis.set_view_interval(1, 10) temp_lf(val) + @pytest.mark.parametrize('val', [1e+323, 2e+323, 10e+323, 11e+323]) + def test_LogFormatter_call_big(self, val): + # test coeff computation in __call__ + temp_lf = mticker.LogFormatter() + temp_lf.create_dummy_axis() + temp_lf(val) + class TestLogitFormatter: @staticmethod From b62f5fc27d403dab0f4b9d7de0df4b81f7a94aae Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Wed, 17 Jul 2024 17:08:08 +0300 Subject: [PATCH 2/4] test_ticker: add a parameter set for np.inf in LogFormatterMathtext --- lib/matplotlib/tests/test_ticker.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 95c61167aba8..e3b5fd846ec2 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1003,6 +1003,7 @@ class TestLogFormatterMathtext: (3, 1e2, '$\\mathdefault{100}$'), (3, 1e-3, '$\\mathdefault{10^{-3}}$'), (3, 1e3, '$\\mathdefault{10^{3}}$'), + (2, np.inf, ''), ] @pytest.mark.parametrize('min_exponent, value, expected', test_data) From 4a59114378e31bbcc3b0b55560250349397a0473 Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Wed, 30 Oct 2024 17:17:54 +0200 Subject: [PATCH 3/4] ticker.py: fix possible np.inf issue with LogFormatter{,Mathtext} --- lib/matplotlib/ticker.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 0053031ece3e..63a519f7933f 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -1001,6 +1001,8 @@ def __call__(self, x, pos=None): return '0' x = abs(x) + if not np.isfinite(x): + return '' b = self._base # only label the decades fx = math.log(x) / math.log(b) @@ -1081,6 +1083,8 @@ def __call__(self, x, pos=None): sign_string = '-' if x < 0 else '' x = abs(x) + if not np.isfinite(x): + return '' b = self._base # only label the decades From 2357bef77aa92633512652a0877e4bc76ef4e163 Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Wed, 29 May 2024 16:21:52 +0300 Subject: [PATCH 4/4] Add test for LogFormatter with very big numbers Reported at https://github.com/matplotlib/matplotlib/pull/27609 . Values < 1e300 that trigger this test failure might be found, but it should be a bit better not to be on the edge of this issue. --- lib/matplotlib/tests/test_ticker.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index e3b5fd846ec2..376c03478032 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1843,6 +1843,27 @@ def test_small_range_loglocator(numticks): assert (np.diff(np.log10(ll.tick_values(6, 150))) == 1).all() +# https://github.com/matplotlib/matplotlib/pull/27609 +# TODO: This test currently fails, as expected and it needs to be fixed... +# To do this completely correctly, we should figure out what are our limits +# for when do integers are interpreted eventually as infinities and why, +# and perhaps always work with np.float128 to increase accuracy as much as +# possible. Eitherway, we should document what is our limit and how it is +# related to the accuracy of np.float{64,128} and Python's native float +# which seems to be the same as np.float64 for this purpose... +def test_LogFormatter_almost_inf(): + fig, ax = plt.subplots() + # TODO: Figure out why 1e400 won't fail, but will make the ax.plot describe + # 1e400 as inf (and hence not print it, and hence not fail). + ax.plot([1, 2], [1, 1e300]) + ax.set_yscale("log") + fig.draw_without_rendering() + almost_inf = ax.get_lines()[0].get_ydata()[1] + # TODO/WIP: remove of course when the above TODOs are fixed. Perhaps assert + # something with ydata... + assert True == False + + def test_NullFormatter(): formatter = mticker.NullFormatter() assert formatter(1.0) == ''