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

Skip to content

Commit bf3585a

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

File tree

4 files changed

+48
-38
lines changed

4 files changed

+48
-38
lines changed

lib/matplotlib/collections.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ 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::
108+
linestyles : {'-', '--', '-.', ':', '', (offset, on-off-seq), ...} \
109+
or list thereof, default: 'solid'
110+
Dash tuples should be of the form::
111111
112112
(offset, onoffseq),
113113
@@ -586,14 +586,15 @@ def set_linestyle(self, ls):
586586
"""
587587
Set the linestyle(s) for the collection.
588588
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-
=========================== =================
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+
========================================== =================
597598
598599
Alternatively a dash tuple of the following form can be provided::
599600
@@ -603,14 +604,12 @@ def set_linestyle(self, ls):
603604
604605
Parameters
605606
----------
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.
607+
ls : {'-', '--', '-.', ':', '', (offset, on-off-seq), ...} \
608+
or list thereof, default: 'solid'
609+
See `.Line2D.set_linestyle` for a complete description.
610610
"""
611611
try:
612612
if isinstance(ls, str):
613-
ls = cbook.ls_mapper.get(ls, ls)
614613
dashes = [mlines._get_dash_pattern(ls)]
615614
else:
616615
try:

lib/matplotlib/lines.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,28 @@
2929
_log = logging.getLogger(__name__)
3030

3131

32-
def _get_dash_pattern(style):
32+
def _get_dash_pattern(style, return_ls=False):
3333
"""Convert linestyle to dash pattern."""
34-
# go from short hand -> full strings
3534
if isinstance(style, str):
36-
style = ls_mapper.get(style, style)
35+
# check valid string
36+
_api.check_in_list([*Line2D._lineStyles, *ls_mapper_r], ls=style)
37+
# go from full strings -> short
38+
style = ls_mapper_r.get(style, style)
39+
# normalize empty style
40+
if style in ('', ' ', 'none'):
41+
style = 'None'
42+
ls = style
43+
else:
44+
ls = '--'
45+
3746
# un-dashed styles
38-
if style in ['solid', 'None']:
47+
if style in ('-', 'None'):
3948
offset = 0
4049
dashes = None
4150
# dashed styles
42-
elif style in ['dashed', 'dashdot', 'dotted']:
51+
elif style in ('--', '-.', ':'):
4352
offset = 0
44-
dashes = tuple(rcParams['lines.{}_pattern'.format(style)])
53+
dashes = tuple(rcParams['lines.{}_pattern'.format(ls_mapper[style])])
4554
#
4655
elif isinstance(style, tuple):
4756
offset, dashes = style
@@ -52,11 +61,22 @@ def _get_dash_pattern(style):
5261

5362
# normalize offset to be positive and shorter than the dash cycle
5463
if dashes is not None:
64+
if any(dash < 0.0 for dash in dashes):
65+
raise ValueError(
66+
"All values in the dash list must be non-negative")
67+
if len(dashes) and not any(dash > 0.0 for dash in dashes):
68+
raise ValueError(
69+
'At least one value in the dash list must be positive')
70+
if offset is None:
71+
raise ValueError(f'Unrecognized linestyle: {style!r}')
5572
dsum = sum(dashes)
5673
if dsum:
5774
offset %= dsum
5875

59-
return offset, dashes
76+
if return_ls:
77+
return (offset, dashes), ls
78+
else:
79+
return offset, dashes
6080

6181

6282
def _scale_dashes(offset, dashes, lw):
@@ -223,6 +243,7 @@ class Line2D(Artist):
223243
'-.': '_draw_dash_dot',
224244
':': '_draw_dotted',
225245
'None': '_draw_nothing',
246+
'none': '_draw_nothing',
226247
' ': '_draw_nothing',
227248
'': '_draw_nothing',
228249
}
@@ -1108,16 +1129,8 @@ def set_linestyle(self, ls):
11081129
11091130
For examples see :doc:`/gallery/lines_bars_and_markers/linestyles`.
11101131
"""
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)
1132+
self._unscaled_dash_pattern, self._linestyle = _get_dash_pattern(ls,
1133+
True)
11211134
self._dash_pattern = _scale_dashes(
11221135
*self._unscaled_dash_pattern, self._linewidth)
11231136
self.stale = True

lib/matplotlib/patches.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,8 @@ def set_linestyle(self, ls):
421421
"""
422422
if ls is None:
423423
ls = "solid"
424-
if ls in [' ', '', 'none']:
425-
ls = 'None'
426-
self._linestyle = ls
427-
self._unscaled_dash_pattern = mlines._get_dash_pattern(ls)
424+
self._unscaled_dash_pattern, self._linestyle = (
425+
mlines._get_dash_pattern(ls, True))
428426
self._dash_pattern = mlines._scale_dashes(
429427
*self._unscaled_dash_pattern, self._linewidth)
430428
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