diff --git a/src/machinevisiontoolbox/ImageWholeFeatures.py b/src/machinevisiontoolbox/ImageWholeFeatures.py index 6c6fdc21..1848af74 100644 --- a/src/machinevisiontoolbox/ImageWholeFeatures.py +++ b/src/machinevisiontoolbox/ImageWholeFeatures.py @@ -1997,7 +1997,10 @@ def plot( # type: ignore[override] thresholds are drawn as vertical lines. """ ax = kwargs.get("ax") - super().plot(type, False, **kwargs) + # block=None: draw but do not show, the figure is shown once, below, after + # the thresholds are added (showing it twice puts two live views of the one + # figure in a notebook, which move together) + super().plot(type, None, **kwargs) # type: ignore[arg-type] if ax is None: ax = plt.gca() ax.axvline(self.th0, color="k", linestyle=":", label=f"th0 = {self.th0:.4g}") diff --git a/tests/test_image_whole_features.py b/tests/test_image_whole_features.py index b0071b41..45e1a1eb 100644 --- a/tests/test_image_whole_features.py +++ b/tests/test_image_whole_features.py @@ -650,5 +650,38 @@ def test_mismatched_histograms(self): h1.compare(h1.pdf) # type: ignore[arg-type] +class TestCannyHistogramPlot(unittest.TestCase): + def setUp(self): + _, self.hist = Image.Read("eiffel-1.png", mono=True).canny(hist=True) + + def tearDown(self): + plt.close("all") + + def test_figure_is_shown_once(self): + # shown twice, a notebook gets two live views of the one figure, which move + # together + with patch("matplotlib.pyplot.show") as show: + self.hist.plot() + self.assertEqual(show.call_count, 1) + + def test_thresholds_are_in_the_figure_when_it_is_shown(self): + labels = [] + + def record(*args, **kwargs): + labels.extend(t.get_text() for t in plt.gca().get_legend().get_texts()) + + with patch("matplotlib.pyplot.show", side_effect=record): + self.hist.plot() + self.assertTrue(any(t.startswith("th0") for t in labels)) + self.assertTrue(any(t.startswith("th1") for t in labels)) + + def test_ax_is_used_and_shown_once(self): + _, ax = plt.subplots() + with patch("matplotlib.pyplot.show") as show: + self.hist.plot(ax=ax) + self.assertEqual(show.call_count, 1) + self.assertEqual(len(plt.get_fignums()), 1) + + if __name__ == "__main__": unittest.main()