Skip to content

Commit eeda705

Browse files
story645timhoffm
andcommitted
support tuple input for sketch
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
1 parent 36a3f7f commit eeda705

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

lib/matplotlib/mpl-data/matplotlibrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,8 @@
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 triplet of the form:
673-
# path.sketch: scale, length, randomness
672+
#path.sketch: None # May be None, or a tuple of the form:
673+
# path.sketch: (scale, length, randomness)
674674
# - *scale* is the amplitude of the wiggle
675675
# perpendicular to the line (in pixels).
676676
# - *length* is the length of the wiggle along the

lib/matplotlib/rcsetup.py

+6-3
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

+7-19
Original file line numberDiff line numberDiff line change
@@ -631,36 +631,24 @@ def test_rcparams_legend_loc_from_file(tmpdir, value):
631631
assert mpl.rcParams["legend.loc"] == value
632632

633633

634-
@pytest.mark.parametrize("value", [(1, 2, 3), '1, 2, 3'])
634+
@pytest.mark.parametrize("value", [(1, 2, 3), '1, 2, 3', '(1, 2, 3)'])
635635
def test_validate_sketch(value):
636636
mpl.rcParams["path.sketch"] = value
637637
assert mpl.rcParams["path.sketch"] == (1, 2, 3)
638638
assert validate_sketch(value) == (1, 2, 3)
639639

640640

641-
@pytest.mark.parametrize("value", [1, '1', '(1, 2, 3)'])
641+
@pytest.mark.parametrize("value", [1, '1', '1 2 3'])
642642
def test_validate_sketch_error(value):
643-
with pytest.raises(ValueError, match="'scale, length, randomness'"):
643+
with pytest.raises(ValueError, match="scale, length, randomness"):
644644
validate_sketch(value)
645-
with pytest.raises(ValueError, match="'scale, length, randomness'"):
645+
with pytest.raises(ValueError, match="scale, length, randomness"):
646646
mpl.rcParams["path.sketch"] = value
647647

648648

649-
def test_rcparams_path_sketch_from_file(tmpdir):
649+
@pytest.mark.parametrize("value", ['1, 2, 3', '(1,2,3)'])
650+
def test_rcparams_path_sketch_from_file(tmpdir, value):
650651
rc_path = tmpdir.join("matplotlibrc")
651-
rc_path.write("path.sketch: 1, 2, 3")
652-
652+
rc_path.write(f"path.sketch: {value}")
653653
with mpl.rc_context(fname=rc_path):
654654
assert mpl.rcParams["path.sketch"] == (1, 2, 3)
655-
656-
657-
def test_rcparams_path_sketch_from_file_error(tmpdir, caplog):
658-
# rcParams parser doesn't read a tuple rcfile entry
659-
rc_path = tmpdir.join("matplotlibrc")
660-
rc_path.write("path.sketch: (1, 2, 3)")
661-
662-
with mpl.rc_context(fname=rc_path):
663-
with caplog.at_level("WARNING"):
664-
assert mpl.rcParams['path.sketch'] is None
665-
assert ("Expected a 'scale, length, randomness' triplet"
666-
in caplog.text)

0 commit comments

Comments
 (0)