Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/users/next_whats_new/nonuniformimage_mousover.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NonUniformImage now has mouseover support
-----------------------------------------
When mousing over a `~matplotlib.image.NonUniformImage` the data values are now
displayed.
16 changes: 11 additions & 5 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,6 @@ def get_cursor_data(self, event):


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

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

def get_cursor_data(self, event):
# docstring inherited
x, y = event.xdata, event.ydata
if (x < self._Ax[0] or x > self._Ax[-1] or
y < self._Ay[0] or y > self._Ay[-1]):
return None
Comment thread
dstansby marked this conversation as resolved.
j = np.searchsorted(self._Ax, x) - 1
i = np.searchsorted(self._Ay, y) - 1
return self._A[i, j]


class PcolorImage(AxesImage):
"""
Expand Down Expand Up @@ -1322,10 +1331,7 @@ def get_cursor_data(self, event):
return None
j = np.searchsorted(self._Ax, x) - 1
i = np.searchsorted(self._Ay, y) - 1
try:
return self._A[i, j]
except IndexError:
return None
return self._A[i, j]


class FigureImage(_ImageBase):
Expand Down
32 changes: 32 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,38 @@ def test_cursor_data():
assert im.get_cursor_data(event) == 44


@pytest.mark.parametrize("xy, data", [
# x/y coords chosen to be 0.5 above boundaries so they lie within image pixels
[[0.5, 0.5], 0 + 0],
[[0.5, 1.5], 0 + 1],
[[4.5, 0.5], 16 + 0],
[[8.5, 0.5], 16 + 0],
[[9.5, 2.5], 81 + 4],
[[-1, 0.5], None],
[[0.5, -1], None],
]
)
def test_cursor_data_nonuniform(xy, data):
from matplotlib.backend_bases import MouseEvent

# Non-linear set of x-values
x = np.array([0, 1, 4, 9, 16])
y = np.array([0, 1, 2, 3, 4])
z = x[np.newaxis, :]**2 + y[:, np.newaxis]**2

fig, ax = plt.subplots()
im = NonUniformImage(ax, extent=(x.min(), x.max(), y.min(), y.max()))
im.set_data(x, y, z)
ax.add_image(im)
# Set lower min lim so we can test cursor outside image
ax.set_xlim(x.min() - 2, x.max())
ax.set_ylim(y.min() - 2, y.max())

xdisp, ydisp = ax.transData.transform(xy)
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
assert im.get_cursor_data(event) == data, (im.get_cursor_data(event), data)


@pytest.mark.parametrize(
"data, text", [
([[10001, 10000]], "[10001.000]"),
Expand Down