Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe4b36b

Browse files
committed
Fix format_cursor_data with nans.
Without this fix, hovering the mouse over a nan pixel in an imshow() would result in `Warning: converting a masked element to nan.` and then `ValueError: cannot convert float NaN to integer`. Fix that. The format_cursor_data test doesn't explicitly check the return value of get_cursor_data anymore because np.ma.masked is returned for nan inputs (this is expected from the general approach for handling invalid data); checking that format_cursor_data gives the right string is sufficient.
1 parent a35f108 commit fe4b36b

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

lib/matplotlib/artist.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,8 @@ def format_cursor_data(self, data):
12731273
# from Artist first and from ScalarMappable second, so
12741274
# Artist.format_cursor_data would always have precedence over
12751275
# ScalarMappable.format_cursor_data.
1276+
if np.ma.getmask(data):
1277+
return "[]"
12761278
n = self.cmap.N
12771279
# Midpoints of neighboring color intervals.
12781280
neighbors = self.norm.inverse(

lib/matplotlib/tests/test_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ def test_cursor_data():
340340
"data, text", [
341341
([[10001, 10000]], "[10001.000]"),
342342
([[.123, .987]], "[0.123]"),
343+
([[np.nan, 1, 2]], "[]"),
343344
])
344345
def test_format_cursor_data(data, text):
345346
from matplotlib.backend_bases import MouseEvent
@@ -349,7 +350,6 @@ def test_format_cursor_data(data, text):
349350

350351
xdisp, ydisp = ax.transData.transform([0, 0])
351352
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
352-
assert im.get_cursor_data(event) == data[0][0]
353353
assert im.format_cursor_data(im.get_cursor_data(event)) == text
354354

355355

0 commit comments

Comments
 (0)