Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63d6321

Browse files
committed
Realized that I didn't have a hatch validator
1 parent 9848661 commit 63d6321

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,19 @@ def __call__(self, s):
6565
% (self.key, s, list(six.itervalues(self.valid))))
6666

6767

68-
def _listify_validator(scalar_validator):
68+
def _listify_validator(scalar_validator, allow_stringlist=False):
6969
def f(s):
7070
if isinstance(s, six.string_types):
7171
try:
7272
return [scalar_validator(v.strip()) for v in s.split(',')
7373
if v.strip()]
7474
except Exception:
75-
# Sometimes, a list of colors might be a single string
76-
# of single-letter colornames. So give that a shot.
77-
return [scalar_validator(v.strip()) for v in s if v.strip()]
75+
if allow_stringlist:
76+
# Sometimes, a list of colors might be a single string
77+
# of single-letter colornames. So give that a shot.
78+
return [scalar_validator(v.strip()) for v in s if v.strip()]
79+
else:
80+
raise
7881
elif type(s) in (list, tuple):
7982
return [scalar_validator(v) for v in s if v]
8083
else:
@@ -331,7 +334,7 @@ def deprecate_axes_colorcycle(value):
331334
return validate_colorlist(value)
332335

333336

334-
validate_colorlist = _listify_validator(validate_color)
337+
validate_colorlist = _listify_validator(validate_color, allow_stringlist=True)
335338
validate_colorlist.__doc__ = 'return a list of colorspecs'
336339

337340
validate_stringlist = _listify_validator(six.text_type)
@@ -583,6 +586,24 @@ def __call__(self, s):
583586
validate_grid_axis = ValidateInStrings('axes.grid.axis', ['x', 'y', 'both'])
584587

585588

589+
def validate_hatch(s):
590+
"""
591+
Validate a hatch pattern.
592+
A hatch pattern string can have any sequence of the following
593+
characters: ``\\ / | - + * . x o O``.
594+
595+
"""
596+
if not isinstance(s, six.text_type):
597+
raise ValueError("Hatch pattern must be a string")
598+
unique_chars = set(s)
599+
unknown = (unique_chars -
600+
set(['\\', '/', '|', '-', '+', '*', '.', 'x', 'o', 'O']))
601+
if unknown:
602+
raise ValueError("Unknown hatch symbol(s): %s" % list(unknown))
603+
return s
604+
validate_hatchlist = _listify_validator(validate_hatch)
605+
606+
586607
_prop_validators = {
587608
'color': validate_colorlist,
588609
'linewidth': validate_floatlist,
@@ -598,6 +619,7 @@ def __call__(self, s):
598619
'markeredgecolor': validate_colorlist,
599620
'alpha': validate_floatlist,
600621
'marker': validate_stringlist,
622+
'hatch': validate_hatchlist,
601623
}
602624
_prop_aliases = {
603625
'c': 'color',
@@ -611,6 +633,7 @@ def __call__(self, s):
611633
'ms': 'markersize',
612634
}
613635

636+
614637
def cycler(*args, **kwargs):
615638
"""
616639
Creates a :class:`cycler.Cycler` object much like :func:`cycler.cycler`,

lib/matplotlib/tests/test_rcparams.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
validate_bool,
2626
validate_nseq_int,
2727
validate_nseq_float,
28-
validate_cycler)
28+
validate_cycler,
29+
validate_hatch)
2930

3031

3132
mpl.rc('text', usetex=False)
@@ -335,6 +336,13 @@ def test_validators():
335336
("cycler(lw=['a', 'b', 'c'])", ValueError), # invalid values
336337
)
337338
},
339+
{'validator': validate_hatch,
340+
'success': (('--|', '--|'), ('\\oO', '\\oO'),
341+
('/+*/.x', '/+*/.x'), ('', '')),
342+
'fail': (('--_', ValueError),
343+
(8, ValueError),
344+
('X', ValueError)),
345+
},
338346
)
339347

340348
for validator_dict in validation_tests:

0 commit comments

Comments
 (0)