From 1b56d96a1fbe0af2b8e0eec8a2aa071ba792a982 Mon Sep 17 00:00:00 2001 From: Jaroza727 Date: Fri, 11 Dec 2020 20:47:07 -0500 Subject: [PATCH 1/7] added format_cursor_data function to NonUniformImage class --- lib/matplotlib/image.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 0ab02c403d0d..222896943d77 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -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): """ @@ -1190,6 +1189,19 @@ 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 + j = np.searchsorted(self._Ax, x) - 1 + i = np.searchsorted(self._Ay, y) - 1 + try: + return self._A[i, j] + except IndexError: + return None + class PcolorImage(AxesImage): """ From 2c8bb0d00b5be1c5b738941edd9b766f1d15862e Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 11 Jan 2024 21:10:00 +0000 Subject: [PATCH 2/7] Add NonUniformImage cursor format test --- lib/matplotlib/tests/test_image.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 9a40b9baa313..8de9ee79bfb3 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -365,6 +365,33 @@ def test_cursor_data(): assert im.get_cursor_data(event) == 44 +@pytest.mark.parametrize("xy, data", [ + [[0.5, 0.5], 0 + 0], + [[0.5, 1.5], 0 + 1], + [[4.5, 0.5], 16 + 0], + [[9.5, 2.5], 81 + 4] + ] +) +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) + ax.set_xlim(x.min(), x.max()) + ax.set_ylim(y.min(), 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]"), From 99f7b7d6c6168ccdba6361c105b3b760bc95a03b Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 11 Jan 2024 21:11:24 +0000 Subject: [PATCH 3/7] Add note on xy test values --- lib/matplotlib/tests/test_image.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 8de9ee79bfb3..daeb1877fd67 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -366,6 +366,7 @@ def test_cursor_data(): @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], From 381cacf74afffff5da425d94e14fbf922b3209ea Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 11 Jan 2024 21:14:18 +0000 Subject: [PATCH 4/7] Add extra test case --- lib/matplotlib/tests/test_image.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index daeb1877fd67..638fd5696acf 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -370,6 +370,7 @@ def test_cursor_data(): [[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] ] ) From 2a81372cd30a69fbe5eaa2599bd71554a357789b Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 14 Jan 2024 16:29:45 +0000 Subject: [PATCH 5/7] Test out of bounds --- lib/matplotlib/tests/test_image.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 638fd5696acf..17d0c011ba63 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -371,7 +371,9 @@ def test_cursor_data(): [[0.5, 1.5], 0 + 1], [[4.5, 0.5], 16 + 0], [[8.5, 0.5], 16 + 0], - [[9.5, 2.5], 81 + 4] + [[9.5, 2.5], 81 + 4], + [[-1, 0.5], None], + [[0.5, -1], None], ] ) def test_cursor_data_nonuniform(xy, data): @@ -386,8 +388,9 @@ def test_cursor_data_nonuniform(xy, data): im = NonUniformImage(ax, extent=(x.min(), x.max(), y.min(), y.max())) im.set_data(x, y, z) ax.add_image(im) - ax.set_xlim(x.min(), x.max()) - ax.set_ylim(y.min(), y.max()) + # 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) From ed712e35ade7f7865119902d2703aeffa732bc62 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 14 Jan 2024 16:35:30 +0000 Subject: [PATCH 6/7] Add what's new --- doc/users/next_whats_new/nonuniformimage_mousover.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 doc/users/next_whats_new/nonuniformimage_mousover.rst diff --git a/doc/users/next_whats_new/nonuniformimage_mousover.rst b/doc/users/next_whats_new/nonuniformimage_mousover.rst new file mode 100644 index 000000000000..e5a7ab1bd155 --- /dev/null +++ b/doc/users/next_whats_new/nonuniformimage_mousover.rst @@ -0,0 +1,4 @@ +NonUniformImage now has mouseover support +----------------------------------------- +When mousing over a `~matplotlib.image.NonUniformImage` the data values are now +displayed. From 291c97796baf6239a996b1563af930bf9e1d17e3 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 22 Jan 2024 21:21:16 +0000 Subject: [PATCH 7/7] Remove extra index error checks --- lib/matplotlib/image.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 222896943d77..e3269062699c 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -1197,10 +1197,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 PcolorImage(AxesImage): @@ -1334,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):