From 461f373822086e12d46f35f6e284e54e2a338046 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Tue, 24 Nov 2026 06:57:41 +1000 Subject: [PATCH] fix: Blobs.plot_labelbox and plot_box say when a format string has a marker plot_labelbox handed its format string to the base function as well as in the options, so plot_labelbox('o') failed with 'Unrecognized linestyle', and a marker on a box failed inside Matplotlib. Pass the options only once, and make mpl_styling refuse a marker when the artist is a Patch. Re-enable the plot_box tests that were skipped. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageBlobs.py | 5 ++- src/machinevisiontoolbox/base/graphics.py | 7 +++- tests/base/test_mpl_styling.py | 17 +++++++++ tests/test_image_blobs.py | 42 ++++++++++++++++++----- 4 files changed, 58 insertions(+), 13 deletions(-) diff --git a/src/machinevisiontoolbox/ImageBlobs.py b/src/machinevisiontoolbox/ImageBlobs.py index 13ce5abc..645f7301 100644 --- a/src/machinevisiontoolbox/ImageBlobs.py +++ b/src/machinevisiontoolbox/ImageBlobs.py @@ -1910,9 +1910,8 @@ def plot_labelbox( fmt, dict(color="yellow", linewidth=2), kwargs, artist=Patch ) for blob in self: - plot_labelbox( - text=label.format(blob.id), fmt=fmt, lrbt=blob.bbox, **options - ) + # the format string is already in options, so is not passed again + plot_labelbox(text=label.format(blob.id), lrbt=blob.bbox, **options) def plot_centroid(self, *args: Any, label: bool = False, **kwargs: Any) -> None: """ diff --git a/src/machinevisiontoolbox/base/graphics.py b/src/machinevisiontoolbox/base/graphics.py index 771bee0e..901b179d 100644 --- a/src/machinevisiontoolbox/base/graphics.py +++ b/src/machinevisiontoolbox/base/graphics.py @@ -14,7 +14,7 @@ import spatialmath.base as smb from matplotlib import cbook, colors from matplotlib.artist import Artist -from matplotlib.patches import Rectangle +from matplotlib.patches import Patch, Rectangle from machinevisiontoolbox.base import name2color @@ -1188,6 +1188,11 @@ def function(*fmt, ..., **kwargs): """ res = parse_fmt(fmt) + if artist is not None and issubclass(artist, Patch) and "marker" in res: + shown = fmt[0] if isinstance(fmt, (tuple, list)) else fmt + raise ValueError( + f"format string {shown!r} has a marker, which a {artist.__name__} cannot draw" + ) if artist is not None: defaults = cbook.normalize_kwargs(defaults or {}, artist) diff --git a/tests/base/test_mpl_styling.py b/tests/base/test_mpl_styling.py index c0539d30..23ec52bb 100644 --- a/tests/base/test_mpl_styling.py +++ b/tests/base/test_mpl_styling.py @@ -145,3 +145,20 @@ def test_dashes_keep_the_default_color(): "color": "red", "linestyle": "--", } + + +def test_a_marker_in_the_format_string_is_refused_for_a_patch(): + from matplotlib.patches import Patch + + with pytest.raises(ValueError, match="cannot draw"): + mpl_styling("r.", {}, {}, artist=Patch) + with pytest.raises(ValueError, match="'o'"): + mpl_styling(("o",), {}, {}, artist=Patch) # as *args arrives + # a line style, a color and a whole color name are fine + assert mpl_styling("r--", {}, {}, artist=Patch) == { + "color": "r", + "linestyle": "--", + } + # and a marker is fine for a line, or when no artist is given + assert mpl_styling("r.", {}, {}, artist=Line2D)["marker"] == "." + assert mpl_styling("r.", {}, {})["marker"] == "." diff --git a/tests/test_image_blobs.py b/tests/test_image_blobs.py index 2c1f581a..12958011 100644 --- a/tests/test_image_blobs.py +++ b/tests/test_image_blobs.py @@ -7,6 +7,7 @@ import matplotlib matplotlib.use("Agg") # non-interactive backend; must precede pyplot import +import matplotlib.colors as mcolors import matplotlib.pyplot as plt import numpy as np import numpy.testing as nt @@ -700,26 +701,49 @@ def _patches(ax): return len(ax.patches) # ------------------------------------------------------------------ # - # plot_box — SKIPPED; positional fmt arg is broken, upstream PR pending - # TODO: remove skips once the PR is merged into the release + # plot_box and plot_labelbox (one Rectangle patch per blob) # ------------------------------------------------------------------ # - @unittest.skip( - "plot_box(): passing a positional fmt argument is broken in the current " - "release. An upstream PR has been submitted but not yet merged. " - "Revisit and remove this skip once the fix is distributed." - ) def test_plot_box_ratio(self): self._test_plot_ratio("plot_box", self._patches) - @unittest.skip("plot_box(): see test_plot_box_ratio for details") def test_plot_box_fmt(self): self._test_plot_adds("plot_box", self._patches, "r--") - @unittest.skip("plot_box(): see test_plot_box_ratio for details") def test_plot_box_kwargs(self): self._test_plot_adds("plot_box", self._patches, color="yellow") + def _box_patch(self, method, *args, **kwargs): + fig, ax = self._fresh_ax() + getattr(self.blobs1, method)(*args, **kwargs) + patch = ax.patches[0] + plt.close(fig) + return patch + + def test_box_format_strings(self): + for method in ("plot_box", "plot_labelbox"): + for fmt, color, linestyle in [ + ("r--", (1, 0, 0), "--"), + ("g", (0, 0.5, 0), "solid"), + ("C1:", mcolors.to_rgb("C1"), ":"), + ("orange", mcolors.to_rgb("orange"), "solid"), + ("--", mcolors.to_rgb("yellow"), "--"), # the default color + ]: + with self.subTest(method=method, fmt=fmt): + patch = self._box_patch(method, fmt) + self.assertEqual(tuple(patch.get_edgecolor()[:3]), tuple(color)) + self.assertEqual(patch.get_linestyle(), linestyle) + + def test_box_with_a_marker_in_the_format_string_says_so(self): + for method in ("plot_box", "plot_labelbox"): + for fmt in ("o", "r."): + with self.subTest(method=method, fmt=fmt): + with self.assertRaisesRegex(ValueError, "cannot draw"): + self._box_patch(method, fmt) + + def test_plot_labelbox_ratio(self): + self._test_plot_ratio("plot_labelbox", self._patches) + # ------------------------------------------------------------------ # # plot_centroid (default: marker=["bx","bo"] → 2 Line2D per blob) # ------------------------------------------------------------------ #