2525import warnings
2626import re
2727
28- from matplotlib .cbook import mplDeprecation
28+ from matplotlib .cbook import mplDeprecation , deprecated , ls_mapper
2929from matplotlib .fontconfig_pattern import parse_fontconfig_pattern
3030from matplotlib .colors import is_color_like
3131
@@ -530,20 +530,28 @@ def validate_ps_distiller(s):
530530 'top' , 'none' ])
531531validate_fillstylelist = _listify_validator (validate_fillstyle )
532532
533- validate_negative_linestyle = ValidateInStrings ('negative_linestyle' ,
534- ['solid' , 'dashed' ],
535- ignorecase = True )
533+ _validate_negative_linestyle = ValidateInStrings ('negative_linestyle' ,
534+ ['solid' , 'dashed' ],
535+ ignorecase = True )
536536
537537
538+ @deprecated ('2.1' ,
539+ addendum = (" See 'validate_negative_linestyle_legacy' " +
540+ "deprecation warning for more information." ))
541+ def validate_negative_linestyle (s ):
542+ return _validate_negative_linestyle (s )
543+
544+
545+ @deprecated ('2.1' ,
546+ addendum = (" The 'contour.negative_linestyle' rcParam now " +
547+ "follows the same validation as the other rcParams " +
548+ "that are related to line style." ))
538549def validate_negative_linestyle_legacy (s ):
539550 try :
540551 res = validate_negative_linestyle (s )
541552 return res
542553 except ValueError :
543554 dashes = validate_nseq_float (2 )(s )
544- warnings .warn ("Deprecated negative_linestyle specification; use "
545- "'solid' or 'dashed'" ,
546- mplDeprecation )
547555 return (0 , dashes ) # (offset, (solid, blank))
548556
549557
@@ -888,6 +896,39 @@ def validate_animation_writer_path(p):
888896 modules ["matplotlib.animation" ].writers .set_dirty ()
889897 return p
890898
899+ # A validator dedicated to the named line styles, based on the items in
900+ # ls_mapper, and a list of possible strings read from Line2D.set_linestyle
901+ _validate_named_linestyle = ValidateInStrings ('linestyle' ,
902+ list (six .iterkeys (ls_mapper )) +
903+ list (six .itervalues (ls_mapper )) +
904+ ['None' , 'none' , ' ' , '' ],
905+ ignorecase = True )
906+
907+
908+ def _validate_linestyle (ls ):
909+ """
910+ A validator for all possible line styles, the named ones *and*
911+ the on-off ink sequences.
912+ """
913+ # Named line style, like u'--' or u'solid'
914+ if isinstance (ls , six .text_type ):
915+ return _validate_named_linestyle (ls )
916+
917+ # On-off ink (in points) sequence *of even length*.
918+ # Offset is set to None.
919+ try :
920+ if len (ls ) % 2 != 0 :
921+ # Expecting a sequence of even length
922+ raise ValueError
923+ return (None , validate_nseq_float ()(ls ))
924+ except (ValueError , TypeError ):
925+ # TypeError can be raised by wrong types passed to float()
926+ # (called inside the instance of validate_nseq_float).
927+ pass
928+
929+ raise ValueError ("linestyle must be a string or " +
930+ "an even-length sequence of floats." )
931+
891932
892933# a map from key -> value, converter
893934defaultParams = {
@@ -912,7 +953,7 @@ def validate_animation_writer_path(p):
912953
913954 # line props
914955 'lines.linewidth' : [1.5 , validate_float ], # line width in points
915- 'lines.linestyle' : ['-' , six . text_type ], # solid line
956+ 'lines.linestyle' : ['-' , _validate_linestyle ], # solid line
916957 'lines.color' : ['C0' , validate_color ], # first color in color cycle
917958 'lines.marker' : ['None' , six .text_type ], # marker name
918959 'lines.markeredgewidth' : [1.0 , validate_float ],
@@ -961,31 +1002,31 @@ def validate_animation_writer_path(p):
9611002 'boxplot.flierprops.markerfacecolor' : ['none' , validate_color_or_auto ],
9621003 'boxplot.flierprops.markeredgecolor' : ['k' , validate_color ],
9631004 'boxplot.flierprops.markersize' : [6 , validate_float ],
964- 'boxplot.flierprops.linestyle' : ['none' , six . text_type ],
1005+ 'boxplot.flierprops.linestyle' : ['none' , _validate_linestyle ],
9651006 'boxplot.flierprops.linewidth' : [1.0 , validate_float ],
9661007
9671008 'boxplot.boxprops.color' : ['k' , validate_color ],
9681009 'boxplot.boxprops.linewidth' : [1.0 , validate_float ],
969- 'boxplot.boxprops.linestyle' : ['-' , six . text_type ],
1010+ 'boxplot.boxprops.linestyle' : ['-' , _validate_linestyle ],
9701011
9711012 'boxplot.whiskerprops.color' : ['k' , validate_color ],
9721013 'boxplot.whiskerprops.linewidth' : [1.0 , validate_float ],
973- 'boxplot.whiskerprops.linestyle' : ['-' , six . text_type ],
1014+ 'boxplot.whiskerprops.linestyle' : ['-' , _validate_linestyle ],
9741015
9751016 'boxplot.capprops.color' : ['k' , validate_color ],
9761017 'boxplot.capprops.linewidth' : [1.0 , validate_float ],
977- 'boxplot.capprops.linestyle' : ['-' , six . text_type ],
1018+ 'boxplot.capprops.linestyle' : ['-' , _validate_linestyle ],
9781019
9791020 'boxplot.medianprops.color' : ['C1' , validate_color ],
9801021 'boxplot.medianprops.linewidth' : [1.0 , validate_float ],
981- 'boxplot.medianprops.linestyle' : ['-' , six . text_type ],
1022+ 'boxplot.medianprops.linestyle' : ['-' , _validate_linestyle ],
9821023
9831024 'boxplot.meanprops.color' : ['C2' , validate_color ],
9841025 'boxplot.meanprops.marker' : ['^' , six .text_type ],
9851026 'boxplot.meanprops.markerfacecolor' : ['C2' , validate_color ],
9861027 'boxplot.meanprops.markeredgecolor' : ['C2' , validate_color ],
9871028 'boxplot.meanprops.markersize' : [6 , validate_float ],
988- 'boxplot.meanprops.linestyle' : ['--' , six . text_type ],
1029+ 'boxplot.meanprops.linestyle' : ['--' , _validate_linestyle ],
9891030 'boxplot.meanprops.linewidth' : [1.0 , validate_float ],
9901031
9911032 ## font props
@@ -1051,8 +1092,7 @@ def validate_animation_writer_path(p):
10511092 'image.composite_image' : [True , validate_bool ],
10521093
10531094 # contour props
1054- 'contour.negative_linestyle' : ['dashed' ,
1055- validate_negative_linestyle_legacy ],
1095+ 'contour.negative_linestyle' : ['dashed' , _validate_linestyle ],
10561096 'contour.corner_mask' : [True , validate_corner_mask ],
10571097
10581098 # errorbar props
@@ -1215,7 +1255,7 @@ def validate_animation_writer_path(p):
12151255 'ytick.direction' : ['out' , six .text_type ], # direction of yticks
12161256
12171257 'grid.color' : ['#b0b0b0' , validate_color ], # grid color
1218- 'grid.linestyle' : ['-' , six . text_type ], # solid
1258+ 'grid.linestyle' : ['-' , _validate_linestyle ], # solid
12191259 'grid.linewidth' : [0.8 , validate_float ], # in points
12201260 'grid.alpha' : [1.0 , validate_float ],
12211261
0 commit comments