Skip to content

Commit 1d6f09f

Browse files
authored
Merge pull request #26921 from story645/mplrc
MNT: clarify path.sketch rcparam format + test validate_sketch
2 parents 8500879 + eeda705 commit 1d6f09f

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -669,14 +669,14 @@
669669
#path.snap: True # When True, rectilinear axis-aligned paths will be snapped
670670
# to the nearest pixel when certain criteria are met.
671671
# When False, paths will never be snapped.
672-
#path.sketch: None # May be None, or a 3-tuple of the form:
673-
# (scale, length, randomness).
674-
# - *scale* is the amplitude of the wiggle
675-
# perpendicular to the line (in pixels).
676-
# - *length* is the length of the wiggle along the
677-
# line (in pixels).
678-
# - *randomness* is the factor by which the length is
679-
# randomly scaled.
672+
#path.sketch: None # May be None, or a tuple of the form:
673+
# path.sketch: (scale, length, randomness)
674+
# - *scale* is the amplitude of the wiggle
675+
# perpendicular to the line (in pixels).
676+
# - *length* is the length of the wiggle along the
677+
# line (in pixels).
678+
# - *randomness* is the factor by which the length is
679+
# randomly scaled.
680680
#path.effects:
681681

682682

lib/matplotlib/rcsetup.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,14 +555,17 @@ def validate_bbox(s):
555555

556556

557557
def validate_sketch(s):
558+
558559
if isinstance(s, str):
559-
s = s.lower()
560+
s = s.lower().strip()
561+
if s.startswith("(") and s.endswith(")"):
562+
s = s[1:-1]
560563
if s == 'none' or s is None:
561564
return None
562565
try:
563566
return tuple(_listify_validator(validate_float, n=3)(s))
564-
except ValueError:
565-
raise ValueError("Expected a (scale, length, randomness) triplet")
567+
except ValueError as exc:
568+
raise ValueError("Expected a (scale, length, randomness) tuple") from exc
566569

567570

568571
def _validate_greaterthan_minushalf(s):

lib/matplotlib/tests/test_rcparams.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
validate_int,
2828
validate_markevery,
2929
validate_stringlist,
30+
validate_sketch,
3031
_validate_linestyle,
3132
_listify_validator)
3233

@@ -628,3 +629,26 @@ def test_rcparams_legend_loc_from_file(tmpdir, value):
628629

629630
with mpl.rc_context(fname=rc_path):
630631
assert mpl.rcParams["legend.loc"] == value
632+
633+
634+
@pytest.mark.parametrize("value", [(1, 2, 3), '1, 2, 3', '(1, 2, 3)'])
635+
def test_validate_sketch(value):
636+
mpl.rcParams["path.sketch"] = value
637+
assert mpl.rcParams["path.sketch"] == (1, 2, 3)
638+
assert validate_sketch(value) == (1, 2, 3)
639+
640+
641+
@pytest.mark.parametrize("value", [1, '1', '1 2 3'])
642+
def test_validate_sketch_error(value):
643+
with pytest.raises(ValueError, match="scale, length, randomness"):
644+
validate_sketch(value)
645+
with pytest.raises(ValueError, match="scale, length, randomness"):
646+
mpl.rcParams["path.sketch"] = value
647+
648+
649+
@pytest.mark.parametrize("value", ['1, 2, 3', '(1,2,3)'])
650+
def test_rcparams_path_sketch_from_file(tmpdir, value):
651+
rc_path = tmpdir.join("matplotlibrc")
652+
rc_path.write(f"path.sketch: {value}")
653+
with mpl.rc_context(fname=rc_path):
654+
assert mpl.rcParams["path.sketch"] == (1, 2, 3)

0 commit comments

Comments
 (0)