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
5 changes: 4 additions & 1 deletion src/machinevisiontoolbox/ImageWholeFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
33 changes: 33 additions & 0 deletions tests/test_image_whole_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading