diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py index 981365d852be..24aaa349ee05 100644 --- a/lib/matplotlib/artist.py +++ b/lib/matplotlib/artist.py @@ -1365,7 +1365,9 @@ def format_cursor_data(self, data): delta = np.diff( self.norm.boundaries[neigh_idx:cur_idx + 2] ).max() - + elif self.norm.vmin == self.norm.vmax: + # singular norms, use delta of 10% of only value + delta = np.abs(self.norm.vmin * .1) else: # Midpoints of neighboring color intervals. neighbors = self.norm.inverse( diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index e4f60aac37a8..2411784af3ec 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -2252,6 +2252,10 @@ def _g_sig_digits(value, delta): it is known with an error of *delta*. """ if delta == 0: + if value == 0: + # if both value and delta are 0, np.spacing below returns 5e-324 + # which results in rather silly results + return 3 # 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 = abs(np.spacing(value)) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index dfacfccb3e0e..6de1754f9ed7 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -390,7 +390,8 @@ def test_cursor_data_nonuniform(xy, data): ([[.123, .987]], "[0.123]"), ([[np.nan, 1, 2]], "[]"), ([[1, 1+1e-15]], "[1.0000000000000000]"), - ([[-1, -1]], "[-1.0000000000000000]"), + ([[-1, -1]], "[-1.0]"), + ([[0, 0]], "[0.00]"), ]) def test_format_cursor_data(data, text): from matplotlib.backend_bases import MouseEvent