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

Skip to content

Commit 5e0ca92

Browse files
committed
FIX: add custom validator for hist.bins
1 parent 7709922 commit 5e0ca92

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,23 @@ def validate_cycler(s):
770770
return cycler_inst
771771

772772

773+
def validate_hist_bins(s):
774+
if isinstance(s, six.text_type) and s == 'auto':
775+
return s
776+
try:
777+
return int(s)
778+
except (TypeError, ValueError):
779+
pass
780+
781+
try:
782+
return validate_floatlist(s)
783+
except ValueError:
784+
pass
785+
786+
raise ValueError("'hist.bins' must be 'auto', an int or " +
787+
"a sequence of floats")
788+
789+
773790
# a map from key -> value, converter
774791
defaultParams = {
775792
'backend': ['Agg', validate_backend], # agg is certainly
@@ -814,7 +831,7 @@ def validate_cycler(s):
814831
'patch.antialiased': [True, validate_bool], # antialiased (no jaggies)
815832

816833
## Histogram properties
817-
'hist.bins': [10, validate_any],
834+
'hist.bins': [10, validate_hist_bins],
818835

819836
## Boxplot properties
820837
'boxplot.notch': [False, validate_bool],

lib/matplotlib/tests/test_rcparams.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
validate_nseq_int,
2828
validate_nseq_float,
2929
validate_cycler,
30-
validate_hatch)
30+
validate_hatch,
31+
validate_hist_bins)
3132

3233

3334
mpl.rc('text', usetex=False)
@@ -363,7 +364,17 @@ def test_validators():
363364
),
364365
'fail': (('fish', ValueError),
365366
),
366-
}
367+
},
368+
{'validator': validate_hist_bins,
369+
'success': (('auto', 'auto'),
370+
('10', 10),
371+
('1, 2, 3', [1, 2, 3]),
372+
([1, 2, 3], [1, 2, 3]),
373+
(np.arange(15), np.arange(15))
374+
),
375+
'fail': (('aardvark', ValueError),
376+
)
377+
}
367378
)
368379

369380
for validator_dict in validation_tests:

0 commit comments

Comments
 (0)