Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6b9ff9c

Browse filesBrowse files
committed
Merge pull request #4961 from WeatherGod/fix_4957
FIX: Bounds checking for get_cursor_data(). Closes #4957
2 parents 6293f20 + 815f679 commit 6b9ff9c
Copy full SHA for 6b9ff9c

File tree

Expand file treeCollapse file tree

2 files changed

+47
-6
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+47
-6
lines changed

‎lib/matplotlib/image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/image.py
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,11 +718,15 @@ def get_cursor_data(self, event):
718718
arr = self.get_array()
719719
data_extent = mtransforms.Bbox([[ymin, xmin], [ymax, xmax]])
720720
array_extent = mtransforms.Bbox([[0, 0], arr.shape[:2]])
721-
trans = mtransforms. BboxTransform(boxin=data_extent,
722-
boxout=array_extent)
721+
trans = mtransforms.BboxTransform(boxin=data_extent,
722+
boxout=array_extent)
723723
y, x = event.ydata, event.xdata
724724
i, j = trans.transform_point([y, x]).astype(int)
725-
return arr[i, j]
725+
# Clip the coordinates at array bounds
726+
if not (0 <= i < arr.shape[0]) or not (0 <= j < arr.shape[1]):
727+
return None
728+
else:
729+
return arr[i, j]
726730

727731

728732
class NonUniformImage(AxesImage):

‎lib/matplotlib/tests/test_image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
+40-3Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,36 @@ def test_cursor_data():
170170
xdisp, ydisp = ax.transData.transform_point([x, y])
171171

172172
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
173-
assert im.get_cursor_data(event) == 44
173+
z = im.get_cursor_data(event)
174+
assert z == 44, "Did not get 44, got %d" % z
175+
176+
# Now try for a point outside the image
177+
# Tests issue #4957
178+
x, y = 10.1, 4
179+
xdisp, ydisp = ax.transData.transform_point([x, y])
180+
181+
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
182+
z = im.get_cursor_data(event)
183+
assert z is None, "Did not get None, got %d" % z
184+
185+
# Hmm, something is wrong here... I get 0, not None...
186+
# But, this works further down in the tests with extents flipped
187+
#x, y = 0.1, -0.1
188+
#xdisp, ydisp = ax.transData.transform_point([x, y])
189+
#event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
190+
#z = im.get_cursor_data(event)
191+
#assert z is None, "Did not get None, got %d" % z
174192

175193
ax.clear()
194+
# Now try with the extents flipped.
176195
im = ax.imshow(np.arange(100).reshape(10, 10), origin='lower')
177196

178197
x, y = 4, 4
179198
xdisp, ydisp = ax.transData.transform_point([x, y])
180199

181200
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
182-
assert im.get_cursor_data(event) == 44
201+
z = im.get_cursor_data(event)
202+
assert z == 44, "Did not get 44, got %d" % z
183203

184204
fig, ax = plt.subplots()
185205
im = ax.imshow(np.arange(100).reshape(10, 10), extent=[0, 0.5, 0, 0.5])
@@ -188,7 +208,24 @@ def test_cursor_data():
188208
xdisp, ydisp = ax.transData.transform_point([x, y])
189209

190210
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
191-
assert im.get_cursor_data(event) == 55
211+
z = im.get_cursor_data(event)
212+
assert z == 55, "Did not get 55, got %d" % z
213+
214+
# Now try for a point outside the image
215+
# Tests issue #4957
216+
x, y = 0.75, 0.25
217+
xdisp, ydisp = ax.transData.transform_point([x, y])
218+
219+
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
220+
z = im.get_cursor_data(event)
221+
assert z is None, "Did not get None, got %d" % z
222+
223+
x, y = 0.01, -0.01
224+
xdisp, ydisp = ax.transData.transform_point([x, y])
225+
226+
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
227+
z = im.get_cursor_data(event)
228+
assert z is None, "Did not get None, got %d" % z
192229

193230

194231
@image_comparison(baseline_images=['image_clip'])

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.