diff --git a/src/machinevisiontoolbox/ImageIO.py b/src/machinevisiontoolbox/ImageIO.py index ae60b246..b73877ae 100644 --- a/src/machinevisiontoolbox/ImageIO.py +++ b/src/machinevisiontoolbox/ImageIO.py @@ -728,7 +728,7 @@ def on_mouse_move(self, event: Any) -> None: if event.inaxes == self.ax2 and self.leftclicked: x, y = event.xdata, event.ydata # update the line positions - self.vertical_line3.set_xdata(x) + self.vertical_line3.set_xdata([x, x]) self.text.set_text("d={:.2f}".format(self.x_left - x)) self.ax2.figure.canvas.draw() # if event.inaxes: @@ -754,10 +754,10 @@ def on_click(self, event: Any) -> None: self.set_cross_hair_visible(True) x, y = event.xdata, event.ydata # update the line positions - self.horizontal_line.set_ydata(y) - self.vertical_line.set_xdata(x) - self.horizontal_line2.set_ydata(y) - self.vertical_line2.set_xdata(x) + self.horizontal_line.set_ydata([y, y]) + self.vertical_line.set_xdata([x, x]) + self.horizontal_line2.set_ydata([y, y]) + self.vertical_line2.set_xdata([x, x]) self.ax.figure.canvas.draw() self.leftclicked = True self.x_left = x @@ -776,6 +776,10 @@ def on_click(self, event: Any) -> None: if interactive: cursor = Cursor(ax1, ax2) + # Matplotlib holds bound-method callbacks weakly, so keep the cursor + # alive for as long as the figure, or the clicks stop working when + # show() does not block (a notebook, or a non-interactive backend) + fig._stdisp_cursor = cursor # type: ignore[attr-defined] fig.canvas.mpl_connect("motion_notify_event", cursor.on_mouse_move) fig.canvas.mpl_connect("button_press_event", cursor.on_click) safe_plt_show(block=True) diff --git a/tests/test_image_io_extra.py b/tests/test_image_io_extra.py index 80fb154e..348a0afc 100644 --- a/tests/test_image_io_extra.py +++ b/tests/test_image_io_extra.py @@ -316,12 +316,6 @@ def event(fig, name, ax, u, v): x, y = ax.transData.transform((u, v)) return MouseEvent(name, fig.canvas, x, y, button=1) - @pytest.mark.xfail( - strict=True, - raises=RuntimeError, - reason="the click handler passes a scalar to Line2D.set_xdata/set_ydata, " - "which Matplotlib 3.11 refuses: 'y must be a sequence'", - ) def test_clicking_places_the_cross_hairs_and_dragging_measures_disparity( self, pair, monkeypatch ): @@ -377,13 +371,6 @@ def user(block=False): assert seen["after"] == ["d=-4.00"] assert seen["dashed"] == pytest.approx(14) - @pytest.mark.xfail( - strict=True, - raises=AssertionError, - reason="stdisp() keeps its cursor object only in a local variable, and " - "Matplotlib holds callbacks weakly, so when plt.show() does not block " - "(a notebook, or a non-interactive backend) the clicks do nothing", - ) def test_cursor_survives_when_show_does_not_block(self, pair): left, right = pair left.stdisp(right, interactive=True)