|
16 | 16 | from matplotlib import verbose |
17 | 17 | from . import artist |
18 | 18 | from .artist import Artist |
19 | | -from .cbook import iterable, is_string_like, is_numlike, ls_mapper |
| 19 | +from .cbook import iterable, is_string_like, is_numlike, ls_mapper_r |
20 | 20 | from .colors import colorConverter |
21 | 21 | from .path import Path |
22 | 22 | from .transforms import Bbox, TransformedPath, IdentityTransform |
@@ -921,55 +921,74 @@ def set_linewidth(self, w): |
921 | 921 | """ |
922 | 922 | self._linewidth = float(w) |
923 | 923 |
|
924 | | - def set_linestyle(self, linestyle): |
| 924 | + def set_linestyle(self, ls): |
925 | 925 | """ |
926 | | - Set the linestyle of the line (also accepts drawstyles) |
| 926 | + Set the linestyle of the line (also accepts drawstyles, |
| 927 | + e.g., ``'steps--'``) |
927 | 928 |
|
928 | 929 |
|
929 | | - ================ ================= |
930 | | - linestyle description |
931 | | - ================ ================= |
932 | | - ``'-'`` solid |
933 | | - ``'--'`` dashed |
934 | | - ``'-.'`` dash_dot |
935 | | - ``':'`` dotted |
936 | | - ``'None'`` draw nothing |
937 | | - ``' '`` draw nothing |
938 | | - ``''`` draw nothing |
939 | | - ================ ================= |
| 930 | + =========================== ================= |
| 931 | + linestyle description |
| 932 | + =========================== ================= |
| 933 | + ``'-'`` or ``'solid'`` solid line |
| 934 | + ``'--'`` or ``'dashed'`` dashed line |
| 935 | + ``'-.'`` or ``'dash_dot'`` dash-dotted line |
| 936 | + ``':'`` or ``'dotted'`` dotted line |
| 937 | + ``'None'`` draw nothing |
| 938 | + ``' '`` draw nothing |
| 939 | + ``''`` draw nothing |
| 940 | + =========================== ================= |
940 | 941 |
|
941 | 942 | 'steps' is equivalent to 'steps-pre' and is maintained for |
942 | 943 | backward-compatibility. |
943 | 944 |
|
| 945 | + Alternatively a dash tuple of the following form can be provided:: |
| 946 | +
|
| 947 | + (offset, onoffseq), |
| 948 | +
|
| 949 | + where ``onoffseq`` is an even length tuple of on and off ink |
| 950 | + in points. |
| 951 | +
|
944 | 952 | .. seealso:: |
945 | 953 |
|
946 | 954 | :meth:`set_drawstyle` |
947 | 955 | To set the drawing style (stepping) of the plot. |
948 | 956 |
|
949 | | - ACCEPTS: [``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'None'`` | |
950 | | - ``' '`` | ``''``] |
951 | | -
|
952 | | - and any drawstyle in combination with a linestyle, e.g., ``'steps--'``. |
| 957 | + Parameters |
| 958 | + ---------- |
| 959 | + ls : { '-', '--', '-.', ':'} and more see description |
| 960 | + The line style. |
953 | 961 | """ |
| 962 | + if not is_string_like(ls): |
| 963 | + if len(ls) != 2: |
| 964 | + raise ValueError() |
| 965 | + |
| 966 | + self.set_dashes(ls[1]) |
| 967 | + self._linestyle = "--" |
| 968 | + return |
954 | 969 |
|
955 | 970 | for ds in self.drawStyleKeys: # long names are first in the list |
956 | | - if linestyle.startswith(ds): |
| 971 | + if ls.startswith(ds): |
957 | 972 | self.set_drawstyle(ds) |
958 | | - if len(linestyle) > len(ds): |
959 | | - linestyle = linestyle[len(ds):] |
| 973 | + if len(ls) > len(ds): |
| 974 | + ls = ls[len(ds):] |
960 | 975 | else: |
961 | | - linestyle = '-' |
| 976 | + ls = '-' |
962 | 977 | break |
963 | 978 |
|
964 | | - if linestyle not in self._lineStyles: |
965 | | - if linestyle in ls_mapper: |
966 | | - linestyle = ls_mapper[linestyle] |
967 | | - else: |
968 | | - verbose.report('Unrecognized line style %s, %s' % |
969 | | - (linestyle, type(linestyle))) |
970 | | - if linestyle in [' ', '']: |
971 | | - linestyle = 'None' |
972 | | - self._linestyle = linestyle |
| 979 | + if ls in [' ', '', 'none']: |
| 980 | + ls = 'None' |
| 981 | + |
| 982 | + if ls not in self._lineStyles: |
| 983 | + try: |
| 984 | + ls = ls_mapper_r[ls] |
| 985 | + except KeyError: |
| 986 | + raise ValueError(("You passed in an invalid linestyle, " |
| 987 | + "`{}`. See " |
| 988 | + "docs of Line2D.set_linestyle for " |
| 989 | + "valid values.").format(ls)) |
| 990 | + |
| 991 | + self._linestyle = ls |
973 | 992 |
|
974 | 993 | @docstring.dedent_interpd |
975 | 994 | def set_marker(self, marker): |
|
0 commit comments