Skip to content
Open
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
1 change: 1 addition & 0 deletions src/machinevisiontoolbox/base/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ def parse_matplotlib_fmt(fmt: str):
marker_match = re.search(marker_pattern, working_fmt)
if marker_match:
res["marker"] = marker_match.group(0)
working_fmt = working_fmt.replace(res["marker"], "", 1)

if len(working_fmt.strip()) > 0:
raise ValueError(
Expand Down
42 changes: 42 additions & 0 deletions tests/base/test_mpl_styling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Compact Matplotlib markers must be consumed before checking leftovers."""

import matplotlib.pyplot as plt
import pytest

from machinevisiontoolbox.base import mpl_styling


@pytest.mark.parametrize(
"fmt, expected",
[
("o", {"marker": "o"}),
("ro--", {"color": "r", "marker": "o", "linestyle": "--"}),
("g^:", {"color": "g", "marker": "^", "linestyle": ":"}),
("-.b*", {"color": "b", "marker": "*", "linestyle": "-."}),
(("ro--",), {"color": "r", "marker": "o", "linestyle": "--"}),
],
)
def test_marker_formats(fmt, expected):
options = mpl_styling(fmt, {}, {})
assert options == expected
fig, ax = plt.subplots()
try:
(line,) = ax.plot([0, 1], [0, 1], **options)
assert line.get_marker() == expected["marker"]
finally:
plt.close(fig)


@pytest.mark.parametrize("fmt", ["roo", "r?", "ro--?"])
def test_unrecognized_components_still_raise(fmt):
with pytest.raises(ValueError, match="Unrecognized format components"):
mpl_styling(fmt, {}, {})


def test_keyword_options_override_compact_marker():
assert mpl_styling("ro--", {"linewidth": 2}, {"marker": "x"}) == {
"color": "r",
"marker": "x",
"linestyle": "--",
"linewidth": 2,
}