Skip to content

Commit bcf63e4

Browse files
fix: consume Matplotlib format markers (#144)
* fix: consume Matplotlib format markers * test: cover compact marker formats and invalid leftovers Check marker formats, tuple input, option precedence and rejection of unmatched characters. --------- Co-authored-by: Peter Corke <peter.i.corke@gmail.com>
1 parent bafa9e9 commit bcf63e4

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎src/machinevisiontoolbox/base/graphics.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,7 @@ def parse_matplotlib_fmt(fmt: str):
10141014
marker_match = re.search(marker_pattern, working_fmt)
10151015
if marker_match:
10161016
res["marker"] = marker_match.group(0)
1017+
working_fmt = working_fmt.replace(res["marker"], "", 1)
10171018

10181019
if len(working_fmt.strip()) > 0:
10191020
raise ValueError(

‎tests/base/test_mpl_styling.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Compact Matplotlib markers must be consumed before checking leftovers."""
2+
3+
import matplotlib.pyplot as plt
4+
import pytest
5+
6+
from machinevisiontoolbox.base import mpl_styling
7+
8+
9+
@pytest.mark.parametrize(
10+
"fmt, expected",
11+
[
12+
("o", {"marker": "o"}),
13+
("ro--", {"color": "r", "marker": "o", "linestyle": "--"}),
14+
("g^:", {"color": "g", "marker": "^", "linestyle": ":"}),
15+
("-.b*", {"color": "b", "marker": "*", "linestyle": "-."}),
16+
(("ro--",), {"color": "r", "marker": "o", "linestyle": "--"}),
17+
],
18+
)
19+
def test_marker_formats(fmt, expected):
20+
options = mpl_styling(fmt, {}, {})
21+
assert options == expected
22+
fig, ax = plt.subplots()
23+
try:
24+
(line,) = ax.plot([0, 1], [0, 1], **options)
25+
assert line.get_marker() == expected["marker"]
26+
finally:
27+
plt.close(fig)
28+
29+
30+
@pytest.mark.parametrize("fmt", ["roo", "r?", "ro--?"])
31+
def test_unrecognized_components_still_raise(fmt):
32+
with pytest.raises(ValueError, match="Unrecognized format components"):
33+
mpl_styling(fmt, {}, {})
34+
35+
36+
def test_keyword_options_override_compact_marker():
37+
assert mpl_styling("ro--", {"linewidth": 2}, {"marker": "x"}) == {
38+
"color": "r",
39+
"marker": "x",
40+
"linestyle": "--",
41+
"linewidth": 2,
42+
}

0 commit comments

Comments
 (0)