diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py index 6c61be641739..179badd3598e 100644 --- a/lib/matplotlib/artist.py +++ b/lib/matplotlib/artist.py @@ -1274,11 +1274,18 @@ def format_cursor_data(self, data): # Artist.format_cursor_data would always have precedence over # ScalarMappable.format_cursor_data. n = self.cmap.N - # Midpoints of neighboring color intervals. - neighbors = self.norm.inverse( - (int(self.norm(data) * n) + np.array([0, 1])) / n) - delta = abs(neighbors - data).max() - return "[{:-#.{}g}]".format(data, cbook._g_sig_digits(data, delta)) + if np.ma.getmask(data): + return "[]" + normed = self.norm(data) + if np.isfinite(normed): + # Midpoints of neighboring color intervals. + neighbors = self.norm.inverse( + (int(self.norm(data) * n) + np.array([0, 1])) / n) + delta = abs(neighbors - data).max() + g_sig_digits = cbook._g_sig_digits(data, delta) + else: + g_sig_digits = 3 # Consistent with default below. + return "[{:-#.{}g}]".format(data, g_sig_digits) else: try: data[0] diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 9aab4e60a7a2..2c4a2763e4c6 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -340,6 +340,7 @@ def test_cursor_data(): "data, text", [ ([[10001, 10000]], "[10001.000]"), ([[.123, .987]], "[0.123]"), + ([[np.nan, 1, 2]], "[]"), ]) def test_format_cursor_data(data, text): from matplotlib.backend_bases import MouseEvent @@ -349,7 +350,6 @@ def test_format_cursor_data(data, text): xdisp, ydisp = ax.transData.transform([0, 0]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) - assert im.get_cursor_data(event) == data[0][0] assert im.format_cursor_data(im.get_cursor_data(event)) == text