diff --git a/lib/matplotlib/mpl-data/matplotlibrc b/lib/matplotlib/mpl-data/matplotlibrc index a097c98429d1..9bd8a622092e 100644 --- a/lib/matplotlib/mpl-data/matplotlibrc +++ b/lib/matplotlib/mpl-data/matplotlibrc @@ -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: diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index fa18677f5895..38d4606024d3 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -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): diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 515068c462d4..65cd823f13a9 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -27,6 +27,7 @@ validate_int, validate_markevery, validate_stringlist, + validate_sketch, _validate_linestyle, _listify_validator) @@ -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)