From 60c38f184132e56b6f781a63322104da0517aee3 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Tue, 24 Nov 2026 07:02:37 +1000 Subject: [PATCH] refactor: apply the format string once in plot_aligned_box, plot_MEC and plot_MER The string was folded into the options by mpl_styling and also passed to spatialmath as *args, so it was parsed twice. The output is the same for every string tried; pin it with a test. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageBlobs.py | 6 +++--- tests/test_image_blobs.py | 27 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/machinevisiontoolbox/ImageBlobs.py b/src/machinevisiontoolbox/ImageBlobs.py index 13ce5abc..147af194 100644 --- a/src/machinevisiontoolbox/ImageBlobs.py +++ b/src/machinevisiontoolbox/ImageBlobs.py @@ -2270,7 +2270,7 @@ def plot_aligned_box(self, *args: Any, **kwargs: Any) -> None: if not isinstance(boxes, list): boxes = [boxes] for box in boxes: - smb.plot_polygon(box[2], *args, close=True, **options) + smb.plot_polygon(box[2], close=True, **options) def plot_MEC(self, *args: Any, **kwargs: Any) -> None: """ @@ -2316,7 +2316,7 @@ def plot_MEC(self, *args: Any, **kwargs: Any) -> None: if not isinstance(mecs, list): mecs = [mecs] for mec in mecs: - smb.plot_circle(mec[2], mec[:2], *args, **options) + smb.plot_circle(mec[2], mec[:2], **options) def plot_MER(self, *args: Any, **kwargs: Any) -> None: """ @@ -2367,7 +2367,7 @@ def plot_MER(self, *args: Any, **kwargs: Any) -> None: box = np.array([[w, h], [w, -h], [-w, -h], [-w, h], [w, h]]).T / 2.0 T = SE2(float(mer[0]), float(mer[1]), float(np.radians(mer[4]))) box = np.asarray(T * box) - smb.plot_polygon(box[:2, :], *args, **options) + smb.plot_polygon(box[:2, :], **options) def label_image(self, image: Any = None) -> Any: """ diff --git a/tests/test_image_blobs.py b/tests/test_image_blobs.py index 2c1f581a..21a96598 100644 --- a/tests/test_image_blobs.py +++ b/tests/test_image_blobs.py @@ -782,6 +782,33 @@ def test_plot_aligned_box_ratio(self): def test_plot_aligned_box_kwargs(self): self._test_plot_adds("plot_aligned_box", self._lines, color="yellow") + # the format string is applied once, by mpl_styling: what each line looks like + _FMT_RESULTS = [ + # fmt, color, linestyle, marker + (None, "yellow", "-", "None"), + ("g", "g", "-", "None"), + ("r--", "r", "--", "None"), + ("--", "yellow", "--", "None"), + ("r:", "r", ":", "None"), + ("o", "yellow", "None", "o"), # markers only, as for plot() + ("r.", "r", "None", "."), + ("C1", "C1", "-", "None"), + ("orange", "orange", "-", "None"), + ] + + def test_polygon_and_circle_methods_apply_the_format_string_once(self): + for method in ("plot_aligned_box", "plot_MEC", "plot_MER"): + for fmt, color, linestyle, marker in self._FMT_RESULTS: + with self.subTest(method=method, fmt=fmt): + fig, ax = self._fresh_ax() + getattr(self.blobs1, method)(*([] if fmt is None else [fmt])) + line = ax.lines[0] + plt.close(fig) + self.assertEqual(line.get_color(), color) + self.assertEqual(line.get_linestyle(), linestyle) + self.assertEqual(line.get_marker(), marker) + self.assertEqual(line.get_linewidth(), 2) + # ------------------------------------------------------------------ # # plot_MEC (smb.plot_circle → 1 artist per blob) # ------------------------------------------------------------------ #