8000 Allow empty linestyle for collections · matplotlib/matplotlib@484b567 · GitHub
[go: up one dir, main page]

Skip to content

Commit 484b567

Browse files
committed
Allow empty linestyle for collections
1 parent f25c2d0 commit 484b567

File tree

4 files changed

+125
-96
lines changed

4 files changed

+125
-96
lines changed

lib/matplotlib/collections.py

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self,
7979
edgecolors=None,
8080
facecolors=None,
8181
linewidths=None,
82-
linestyles='solid',
82+
linestyles='-',
8383
capstyle=None,
8484
joinstyle=None,
8585
antialiaseds=None,
@@ -105,15 +105,8 @@ def __init__(self,
105105
Face color for each patch making up the collection.
106106
linewidths : float or list of floats, default: :rc:`patch.linewidth`
107107
Line width for each patch making up the collection.
108-
linestyles : str or tuple or list thereof, default: 'solid'
109-
Valid strings are ['solid', 'dashed', 'dashdot', 'dotted', '-',
110-
'--', '-.', ':']. Dash tuples should be of the form::
111-
112-
(offset, onoffseq),
113-
114-
where *onoffseq* is an even length tuple of on and off ink lengths
115-
in points. For examples, see
116-
:doc:`/gallery/lines_bars_and_markers/linestyles`.
108+
linestyles : str or tuple or list thereof, default: '-'
109+
Line style or list of line styles. See `set_linestyle` for details.
117110
capstyle : `.CapStyle`-like, default: :rc:`patch.capstyle`
118111
Style to use for capping lines for all paths in the collection.
119112
Allowed values are %(CapStyle)s.
@@ -584,43 +577,51 @@ def set_linewidth(self, lw):
584577

585578
def set_linestyle(self, ls):
586579
"""
587-
Set the linestyle(s) for the collection.
580+
Set the line style(s) for the collection.
588581
589-
=========================== =================
590-
linestyle description
591-
=========================== =================
592-
``'-'`` or ``'solid'`` solid line
593-
``'--'`` or ``'dashed'`` dashed line
594-
``'-.'`` or ``'dashdot'`` dash-dotted line
595-
``':'`` or ``'dotted'`` dotted line
596-
=========================== =================
582+
Parameters
583+
----------
584+
ls : str or tuple or list thereof
585+
The line style. Possible values:
597586
598-
Alternatively a dash tuple of the following form can be provided::
587+
- A string:
599588
600-
(offset, onoffseq),
589+
========================================== =================
590+
linestyle description
591+
========================================== =================
592+
``'-'`` or ``'solid'`` solid line
593+
``'--'`` or ``'dashed'`` dashed line
594+
``'-.'`` or ``'dashdot'`` dash-dotted line
595+
``':'`` or ``'dotted'`` dotted line
596+
``'none'``, ``'None'``, ``' '``, or ``''`` draw nothing
597+
========================================== =================
601598
602-
where ``onoffseq`` is an even length tuple of on and off ink in points.
599+
- Alternatively a dash tuple of the following form can be
600+
provided::
603601
604-
Parameters
605-
----------
606-
ls : str or tuple or list thereof
607-
Valid values for individual linestyles include {'-', '--', '-.',
608-
':', '', (offset, on-off-seq)}. See `.Line2D.set_linestyle` for a
609-
complete description.
602+
(offset, onoffseq)
603+
604+
where ``onoffseq`` is an even length tuple of on and off ink
605+
in points.
606+
607+
If a single value is provided, this applies to all objects in the
608+
collection. A list can be provided to set different line styles to
609+
different objects.
610+
611+
For examples see :doc:`/gallery/lines_bars_and_markers/linestyles`.
612+
613+
The ``'dashed'``, ``'dashdot'``, and ''`dotted'`` line styles are
614+
controlled by :rc:`lines.dashed_pattern`,
615+
:rc:`lines.dashdot_pattern`, and :rc:`lines.dotted_pattern`,
616+
respectively.
610617
"""
611-
try:
612-
if isinstance(ls, str):
613-
ls = cbook.ls_mapper.get(ls, ls)
618+
if isinstance(ls, str):
619+
dashes = [mlines._get_dash_pattern(ls)]
620+
else:
621+
try:
614622
dashes = [mlines._get_dash_pattern(ls)]
615-
else:
616-
try:
617-
dashes = [mlines._get_dash_pattern(ls)]
618-
except ValueError:
619-
dashes = [mlines._get_dash_pattern(x) for x in ls]
620-
621-
except ValueError as err:
622-
raise ValueError('Do not know how to convert {!r} to '
623-
'dashes'.format(ls)) from err
623+
except ValueError:
624+
dashes = [mlines._get_dash_pattern(x) for x in ls]
624625

625626
# get the list of raw 'unscaled' dash patterns
626627
self._us_linestyles = dashes
@@ -1522,7 +1523,7 @@ def __init__(self,
15221523
linelength=1,
15231524
linewidth=None,
15241525
color=None,
1525-
linestyle='solid',
1526+
linestyle='-',
15261527
antialiased=None,
15271528
**kwargs
15281529
):
@@ -1545,14 +1546,8 @@ def __init__(self,
15451546
The line width of the event lines, in points.
15461547
color : color or list of colors, default: :rc:`lines.color`
15471548
The color of the event lines.
1548-
linestyle : str or tuple or list thereof, default: 'solid'
1549-
Valid strings are ['solid', 'dashed', 'dashdot', 'dotted',
1550-
'-', '--', '-.', ':']. Dash tuples should be of the form::
1551-
1552-
(offset, onoffseq),
1553-
1554-
where *onoffseq* is an even length tuple of on and off ink
1555-
in points.
1549+
linestyle : str or tuple or list thereof, default: '-'
1550+
Line style or list of line styles. See `set_linestyle` for details.
15561551
antialiased : bool or list thereof, default: :rc:`lines.antialiased`
15571552
Whether to use antialiasing for drawing the lines.
15581553
**kwargs

lib/matplotlib/lines.py

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,61 @@
2929
_log = logging.getLogger(__name__)
3030

3131

32-
def _get_dash_pattern(style):
33-
"""Convert linestyle to dash pattern."""
34-
# go from short hand -> full strings
32+
def _get_dash_pattern(style, return_linestyle=False):
33+
"""
34+
Convert linestyle to dash pattern.
35+
36+
Return normalized line style if *return_linestyle* is True.
37+
"""
38+
orig_style = style # keep copy for error message
3539
if isinstance(style, str):
36-
style = ls_mapper.get(style, style)
40+
# check valid string
41+
_api.check_in_list([*Line2D._lineStyles, *ls_mapper_r],
42+
linestyle=style)
43+
# go from full strings -> short
44+
style = ls_mapper_r.get(style, style)
45+
# normalize empty style
46+
if style in ('', ' ', 'none'):
47+
style = 'None'
48+
ls = style
49+
else: # style is a dash tuple
50+
ls = '--'
51+
3752
# un-dashed styles
38< 10000 /td>-
if style in ['solid', 'None']:
53+
if style in ('-', 'None'):
3954
offset = 0
4055
dashes = None
4156
# dashed styles
42-
elif style in ['dashed', 'dashdot', 'dotted']:
57+
elif style in ('--', '-.', ':'):
4358
offset = 0
44-
dashes = tuple(rcParams['lines.{}_pattern'.format(style)])
45-
#
59+
dashes = tuple(rcParams[f'lines.{ls_mapper[style]}_pattern'])
60+
# dash tuple
4661
elif isinstance(style, tuple):
4762
offset, dashes = style
4863
if offset is None:
49-
raise ValueError(f'Unrecognized linestyle: {style!r}')
64+
raise ValueError(f'Unrecognized linestyle: {orig_style!r}')
5065
else:
51-
raise ValueError(f'Unrecognized linestyle: {style!r}')
66+
raise ValueError(f'Unrecognized linestyle: {orig_style!r}')
5267

5368
# normalize offset to be positive and shorter than the dash cycle
5469
if dashes is not None:
70+
try:
71+
if any(dash < 0.0 for dash in dashes):
72+
raise ValueError(
73+
"All values in the dash list must be non-negative")
74+
if len(dashes) and not any(dash > 0.0 for dash in dashes):
75+
raise ValueError(
76+
'At least one value in the dash list must be positive')
77+
except TypeError:
78+
raise ValueError(f'Unrecognized linestyle: {orig_style!r}')
5579
dsum = sum(dashes)
5680
if dsum:
5781
offset %= dsum
5882

59-
return offset, dashes
83+
if return_linestyle:
84+
return (offset, dashes), ls
85+
else:
86+
return offset, dashes
6087

6188

6289
def _scale_dashes(offset, dashes, lw):
@@ -223,6 +250,7 @@ class Line2D(Artist):
223250
'-.': '_draw_dash_dot',
224251
':': '_draw_dotted',
225252
'None': '_draw_nothing',
253+
'none': '_draw_nothing',
226254
' ': '_draw_nothing',
227255
'': '_draw_nothing',
228256
}
@@ -1079,12 +1107,12 @@ def set_linewidth(self, w):
10791107

10801108
def set_linestyle(self, ls):
10811109
"""
1082-
Set the linestyle of the line.
1110+
Set the line style of the line.
10831111
10841112
Parameters
10851113
----------
1086-
ls : {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
1087-
Possible values:
1114+
ls : str or tuple
1115+
The line style. Possible values:
10881116
10891117
- A string:
10901118
@@ -1107,17 +1135,14 @@ def set_linestyle(self, ls):
11071135
in points. See also :meth:`set_dashes`.
11081136
11091137
For examples see :doc:`/gallery/lines_bars_and_markers/linestyles`.
1138+
1139+
The ``'dashed'``, ``'dashdot'``, and ''`dotted'`` line styles are
1140+
controlled by :rc:`lines.dashed_pattern`,
1141+
:rc:`lines.dashdot_pattern`, and :rc:`lines.dotted_pattern`,
1142+
respectively.
11101143
"""
1111-
if isinstance(ls, str):
1112-
if ls in [' ', '', 'none']:
1113-
ls = 'None'
1114-
_api.check_in_list([*self._lineStyles, *ls_mapper_r], ls=ls)
1115-
if ls not in self._lineStyles:
1116-
ls = ls_mapper_r[ls]
1117-
self._linestyle = ls
1118-
else:
1119-
self._linestyle = '--'
1120-
self._unscaled_dash_pattern = _get_dash_pattern(ls)
1144+
self._unscaled_dash_pattern, self._linestyle = _get_dash_pattern(ls,
1145+
True)
11211146
self._dash_pattern = _scale_dashes(
11221147
*self._unscaled_dash_pattern, self._linewidth)
11231148
self.stale = True

lib/matplotlib/patches.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -396,35 +396,44 @@ def set_linewidth(self, w):
396396

397397
def set_linestyle(self, ls):
398398
"""
399-
Set the patch linestyle.
399+
Set the patch line style.
400400
401-
========================================== =================
402-
linestyle description
403-
========================================== =================
404-
``'-'`` or ``'solid'`` solid line
405-
``'--'`` or ``'dashed'`` dashed line
406-
``'-.'`` or ``'dashdot'`` dash-dotted line
407-
``':'`` or ``'dotted'`` dotted line
408-
``'none'``, ``'None'``, ``' '``, or ``''`` draw nothing
409-
========================================== =================
401+
Parameters
402+
----------
403+
ls : str or tuple
404+
The line style. Possible values:
410405
411-
Alternatively a dash tuple of the following form can be provided::
406+
- A string:
412407
413-
(offset, onoffseq)
408+
========================================== =================
409+
linestyle description
410+
========================================== =================
411+
``'-'`` or ``'solid'`` solid line
412+
``'--'`` or ``'dashed'`` dashed line
413+
``'-.'`` or ``'dashdot'`` dash-dotted line
414+
``':'`` or ``'dotted'`` dotted line
415+
``'none'``, ``'None'``, ``' '``, or ``''`` draw nothing
416+
========================================== =================
414417
415-
where ``onoffseq`` is an even length tuple of on and off ink in points.
418+
- Alternatively a dash tuple of the following form can be
419+
provided::
416420
417-
Parameters
418-
----------
419-
ls : {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
420-
The line style.
421+
(offset, onoffseq)
422+
423+
where ``onoffseq`` is an even length tuple of on and off ink
424+
in points.
425+
426+
For examples see :doc:`/gallery/lines_bars_and_markers/linestyles`.
427+
428+
The ``'dashed'``, ``'dashdot'``, and ''`dotted'`` line styles are
429+
controlled by :rc:`lines.dashed_pattern`,
430+
:rc:`lines.dashdot_pattern`, and :rc:`lines.dotted_pattern`,
431+
respectively.
421432
"""
422433
if ls is None:
423-
ls = "solid"
424-
if ls in [' ', '', 'none']:
425-
ls = 'None'
426-
self._linestyle = ls
427-
self._unscaled_dash_pattern = mlines._get_dash_pattern(ls)
434+
ls = "-"
435+
self._unscaled_dash_pattern, self._linestyle = (
436+
mlines._get_dash_pattern(ls, True))
428437
self._dash_pattern = mlines._scale_dashes(
429438
*self._unscaled_dash_pattern, self._linewidth)
430439
self.stale = True

lib/matplotlib/tests/test_patches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ def test_default_linestyle():
797797
patch = Patch()
798798
patch.set_linestyle('--')
799799
patch.set_linestyle(None)
800-
assert patch.get_linestyle() == 'solid'
800+
assert patch.get_linestyle() == '-'
801801

802802

803803
def test_default_capstyle():

0 commit comments

Comments
 (0)
0