Skip to content

MNT: clarify path.sketch rcparam format + test validate_sketch #26921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2023
Merged
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
16 changes: 8 additions & 8 deletions lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -669,14 +669,14 @@
#path.snap: True # When True, rectilinear axis-aligned paths will be snapped
# to the nearest pixel when certain criteria are met.
# When False, paths will never be snapped.
#path.sketch: None # May be None, or a 3-tuple of the form:
# (scale, length, randomness).
# - *scale* is the amplitude of the wiggle
# perpendicular to the line (in pixels).
# - *length* is the length of the wiggle along the
# line (in pixels).
# - *randomness* is the factor by which the length is
# randomly scaled.
#path.sketch: None # May be None, or a tuple of the form:
# path.sketch: (scale, length, randomness)
# - *scale* is the amplitude of the wiggle
# perpendicular to the line (in pixels).
# - *length* is the length of the wiggle along the
# line (in pixels).
# - *randomness* is the factor by which the length is
# randomly scaled.
#path.effects:


Expand Down
9 changes: 6 additions & 3 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,14 +555,17 @@ def validate_bbox(s):


def validate_sketch(s):

if isinstance(s, str):
s = s.lower()
s = s.lower().strip()
if s.startswith("(") and s.endswith(")"):
s = s[1:-1]
if s == 'none' or s is None:
return None
try:
return tuple(_listify_validator(validate_float, n=3)(s))
except ValueError:
raise ValueError("Expected a (scale, length, randomness) triplet")
except ValueError as exc:
raise ValueError("Expected a (scale, length, randomness) tuple") from exc


def _validate_greaterthan_minushalf(s):
Expand Down
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
validate_int,
validate_markevery,
validate_stringlist,
validate_sketch,
_validate_linestyle,
_listify_validator)

Expand Down Expand Up @@ -628,3 +629,26 @@ def test_rcparams_legend_loc_from_file(tmpdir, value):

with mpl.rc_context(fname=rc_path):
assert mpl.rcParams["legend.loc"] == value


@pytest.mark.parametrize("value", [(1, 2, 3), '1, 2, 3', '(1, 2, 3)'])
def test_validate_sketch(value):
mpl.rcParams["path.sketch"] = value
assert mpl.rcParams["path.sketch"] == (1, 2, 3)
assert validate_sketch(value) == (1, 2, 3)


@pytest.mark.parametrize("value", [1, '1', '1 2 3'])
def test_validate_sketch_error(value):
with pytest.raises(ValueError, match="scale, length, randomness"):
validate_sketch(value)
with pytest.raises(ValueError, match="scale, length, randomness"):
mpl.rcParams["path.sketch"] = value


@pytest.mark.parametrize("value", ['1, 2, 3', '(1,2,3)'])
def test_rcparams_path_sketch_from_file(tmpdir, value):
rc_path = tmpdir.join("matplotlibrc")
rc_path.write(f"path.sketch: {value}")
with mpl.rc_context(fname=rc_path):
assert mpl.rcParams["path.sketch"] == (1, 2, 3)