From 6dd5b0235147be9b0a0c5dd76b2405625c537cee Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 23 May 2022 19:50:51 +0200 Subject: [PATCH] Fix _g_sig_digits for value<0 and delta=0. np.spacing can return negative values, so we need an abs(). --- lib/matplotlib/cbook/__init__.py | 2 +- lib/matplotlib/tests/test_image.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 5a955ed459c5..f1596aa12f30 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -2158,7 +2158,7 @@ def _g_sig_digits(value, delta): if delta == 0: # delta = 0 may occur when trying to format values over a tiny range; # in that case, replace it by the distance to the closest float. - delta = np.spacing(value) + delta = abs(np.spacing(value)) # If e.g. value = 45.67 and delta = 0.02, then we want to round to 2 digits # after the decimal point (floor(log10(0.02)) = -2); 45.67 contributes 2 # digits before the decimal point (floor(log10(45.67)) + 1 = 2): the total diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index e8e90b768085..d50b598f304f 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -342,6 +342,7 @@ def test_cursor_data(): ([[.123, .987]], "[0.123]"), ([[np.nan, 1, 2]], "[]"), ([[1, 1+1e-15]], "[1.0000000000000000]"), + ([[-1, -1]], "[-1.0000000000000000]"), ]) def test_format_cursor_data(data, text): from matplotlib.backend_bases import MouseEvent