diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index ba61dd5e8415..d2ad5fd8cbf7 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -2208,6 +2208,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 @@ -2221,11 +2224,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):