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

Skip to content

Commit b4e18b2

Browse files
authored
Merge pull request #27640 from dstansby/non-uniform-z
Add `get_cursor_data` to `NonUniformImage`
2 parents 59f9b3c + 291c977 commit b4e18b2

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NonUniformImage now has mouseover support
2+
-----------------------------------------
3+
When mousing over a `~matplotlib.image.NonUniformImage` the data values are now
4+
displayed.

lib/matplotlib/image.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,6 @@ def get_cursor_data(self, event):
10381038

10391039

10401040
class NonUniformImage(AxesImage):
1041-
mouseover = False # This class still needs its own get_cursor_data impl.
10421041

10431042
def __init__(self, ax, *, interpolation='nearest', **kwargs):
10441043
"""
@@ -1190,6 +1189,16 @@ def set_cmap(self, cmap):
11901189
raise RuntimeError('Cannot change colors after loading data')
11911190
super().set_cmap(cmap)
11921191

1192+
def get_cursor_data(self, event):
1193+
# docstring inherited
1194+
x, y = event.xdata, event.ydata
1195+
if (x < self._Ax[0] or x > self._Ax[-1] or
1196+
y < self._Ay[0] or y > self._Ay[-1]):
1197+
return None
1198+
j = np.searchsorted(self._Ax, x) - 1
1199+
i = np.searchsorted(self._Ay, y) - 1
1200+
return self._A[i, j]
1201+
11931202

11941203
class PcolorImage(AxesImage):
11951204
"""
@@ -1322,10 +1331,7 @@ def get_cursor_data(self, event):
13221331
return None
13231332
j = np.searchsorted(self._Ax, x) - 1
13241333
i = np.searchsorted(self._Ay, y) - 1
1325-
try:
1326-
return self._A[i, j]
1327-
except IndexError:
1328-
return None
1334+
return self._A[i, j]
13291335

13301336

13311337
class FigureImage(_ImageBase):

lib/matplotlib/tests/test_image.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,38 @@ def test_cursor_data():
365365
assert im.get_cursor_data(event) == 44
366366

367367

368+
@pytest.mark.parametrize("xy, data", [
369+
# x/y coords chosen to be 0.5 above boundaries so they lie within image pixels
370+
[[0.5, 0.5], 0 + 0],
371+
[[0.5, 1.5], 0 + 1],
372+
[[4.5, 0.5], 16 + 0],
373+
[[8.5, 0.5], 16 + 0],
374+
[[9.5, 2.5], 81 + 4],
375+
[[-1, 0.5], None],
376+
[[0.5, -1], None],
377+
]
378+
)
379+
def test_cursor_data_nonuniform(xy, data):
380+
from matplotlib.backend_bases import MouseEvent
381+
382+
# Non-linear set of x-values
383+
x = np.array([0, 1, 4, 9, 16])
384+
y = np.array([0, 1, 2, 3, 4])
385+
z = x[np.newaxis, :]**2 + y[:, np.newaxis]**2
386+
387+
fig, ax = plt.subplots()
388+
im = NonUniformImage(ax, extent=(x.min(), x.max(), y.min(), y.max()))
389+
im.set_data(x, y, z)
390+
ax.add_image(im)
391+
# Set lower min lim so we can test cursor outside image
392+
ax.set_xlim(x.min() - 2, x.max())
393+
ax.set_ylim(y.min() - 2, y.max())
394+
395+
xdisp, ydisp = ax.transData.transform(xy)
396+
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
397+
assert im.get_cursor_data(event) == data, (im.get_cursor_data(event), data)
398+
399+
368400
@pytest.mark.parametrize(
369401
"data, text", [
370402
([[10001, 10000]], "[10001.000]"),

0 commit comments

Comments
 (0)