Skip to content

fix: accept the short Matplotlib option names (lw, c, ...) in mpl_styling - #266

Merged
petercorke merged 1 commit into
mainfrom
fix/mpl-styling-aliases
Nov 23, 2026
Merged

petercorke merged 1 commit into
mainfrom
fix/mpl-styling-aliases

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Fixes blobs.plot_axes(lw=4) and the same call on the other Blobs.plot_* methods, which raised Matplotlib's "Got both 'linewidth' and 'lw', which are aliases of one another".

mpl_styling merges the defaults and the caller's options by key name. Every Blobs.plot_* default is dict(color=..., linewidth=2), so a caller's lw=4 or c="blue" sat beside the default linewidth or color, and Matplotlib refused the pair.

  • New artist argument. mpl_styling(fmt, defaults, kwargs, artist=Line2D) takes the Matplotlib class that will receive the options (Line2D, Patch, Text, ...). When it is given, defaults and kwargs are first converted to the full option names with matplotlib.cbook.normalize_kwargs, so lw=4 replaces the default linewidth=2. When it is None (the default) nothing changes, so existing callers are unaffected. A caller who gives both names (lw=1, linewidth=2) still gets Matplotlib's error.
  • Blobs. The 9 mpl_styling calls in ImageBlobs.py now pass Line2D (plot_perimeter, plot_ellipse, plot_axes, plot_aligned_box, plot_MEC, plot_MER) or Patch (plot_box, plot_labelbox). The helpers for the ellipse, circle and polygon draw a Line2D, or a filled Polygon patch when filled=True; both accept the full names Line2D converts to.

Tests (13 new, 4 of them in tests/base/): lw, c, ls, ms for Line2D, ec and fc for Patch, ha and size for Text; an alias in the defaults; an alias beating the format string; the merged options accepted by a real plot call; the both-names error; no conversion without artist; None values dropped; and plot_axes, plot_ellipse, plot_perimeter and plot_box called with lw and c or ec. mypy reports no issues and the full suite passes (2198 passed, 33 skipped).

Found while testing, not changed here: when a format string is given, the parts it does not set come back as None and override the matching default, so mpl_styling("--", dict(color="red")) loses the red. That will be a separate bug/ PR.

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

🤖 Generated with Claude Code

…ling

mpl_styling merged its defaults and the caller's options by key, so a caller's
lw=4 sat beside the default linewidth=2 and Matplotlib refused both. Add an
artist argument (Line2D, Patch, Text, ...): when given, the options are first
converted to the full names for that class. The Blobs plot methods now pass it.

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 14 complexity · 0 duplication

Metric Results
Complexity 14
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 7387141 into main Nov 23, 2026
34 checks passed
@petercorke
petercorke deleted the fix/mpl-styling-aliases branch November 23, 2026 20:13
@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