From 3db5500cb242e5c3fa42dd54958bbfe0b264c123 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 10 Jun 2025 22:33:38 -0400 Subject: [PATCH] Fix RuntimeWarning with NaN input to format_cursor_data --- lib/matplotlib/cbook.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 3100cc4da81d..a09780965b0c 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -2228,6 +2228,9 @@ def _g_sig_digits(value, delta): Return the number of significant digits to %g-format *value*, assuming that it is known with an error of *delta*. """ + # For inf or nan, the precision doesn't matter. + if not math.isfinite(value): + return 0 if delta == 0: if value == 0: # if both value and delta are 0, np.spacing below returns 5e-324 @@ -2241,11 +2244,10 @@ def _g_sig_digits(value, delta): # digits before the decimal point (floor(log10(45.67)) + 1 = 2): the total # is 4 significant digits. A value of 0 contributes 1 "digit" before the # decimal point. - # For inf or nan, the precision doesn't matter. return max( 0, (math.floor(math.log10(abs(value))) + 1 if value else 1) - - math.floor(math.log10(delta))) if math.isfinite(value) else 0 + - math.floor(math.log10(delta))) def _unikey_or_keysym_to_mplkey(unikey, keysym):