Skip to content

fix: keep the defaults a MATLAB-style format string does not set in mpl_styling - #267

Merged
petercorke merged 1 commit into
mainfrom
bug/mpl-styling-fmt
Nov 23, 2026
Merged

petercorke merged 1 commit into
mainfrom
bug/mpl-styling-fmt

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Fixes mpl_styling losing the default of anything a MATLAB-style format string does not set, and replaces its hand-written parser with one that follows Matplotlib's rules.

The bug. The old parser returned None for each of color, marker and line style that the string did not set, and that None replaced the default, so mpl_styling("--", dict(color="red")) gave no color. blobs.plot_ellipse("--") drew in the next color of the Matplotlib cycle, not the documented cyan.

The change. parse_fmt in base/graphics.py replaces the nested parser. It returns only the parts the string sets, so defaults | parse_fmt(fmt) | kwargs keeps every default the string leaves alone. It follows plt.plot:

string result
"g" green, default line style
"r--" red dashed line
"r:" red dotted line
"r." red dots only, no line
"--" dashed, default color

It also accepts cycle colors ("C2"), whole-string colors ("red", "#ff8000"), and the markers the old regex missed (X, 8, 1 to 4). It raises ValueError ("... is not a valid format string") for two colors, two markers, two line styles or an unknown character.

Behavior change. A string with a marker but no line style now means markers only, as in plot. For plot_perimeter, plot_ellipse and plot_axes, a string such as "o" or "r." used to give the line with markers on it, and now gives the markers only. The other five Blobs.plot_* methods already did this, because they also hand the string to spatialmath. Strings with no marker, or with a line style, are unchanged.

A copy. parse_fmt is a copy of the function proposed for spatialmath in rai-opensource/spatialmath-python PR 321 (open), which will take months to reach a release. There is a comment at the copy to remove it and import that one once the release is the minimum we require.

Tests: tests/base/test_parse_fmt.py (copied from the spatialmath PR) compares parse_fmt with Matplotlib's own parser for about 6,600 valid strings in every order, every valid pair, whole-string colors and a set of invalid strings; test_mpl_styling.py has new tests that unset parts keep their defaults, and the old tests are updated for the new error text and for "o" giving no line. mypy reports no issues and the full suite passes (2244 passed, 33 skipped).

AI-assisted: written with Claude Code (Claude Sonnet 5.5) and reviewed by the maintainer.

🤖 Generated with Claude Code

…pl_styling

The parser gave None for each part the string did not set, and that None
replaced the default, so plot_ellipse('--') lost its default color. Replace it
with parse_fmt, a copy of the function proposed for spatialmath (PR 321), which
returns only what the string sets and follows plot(): a marker with no line
style means no line. Tested against Matplotlib's own parser.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 54 complexity · 0 duplication

Metric Results
Complexity 54
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@petercorke
petercorke merged commit 1dda16e into main Nov 23, 2026
34 checks passed
@petercorke
petercorke deleted the bug/mpl-styling-fmt branch November 23, 2026 20:36
@github-actions github-actions Bot mentioned this pull request Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant