Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 15f37d8

Browse filesBrowse files
committed
Allow empty linestyle for collections
1 parent f25c2d0 commit 15f37d8
Copy full SHA for 15f37d8

File tree

3 files changed

+45
-36
lines changed
Filter options

3 files changed

+45
-36
lines changed

‎lib/matplotlib/collections.py

Copy file name to clipboardExpand all lines: lib/matplotlib/collections.py
+15-16Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/lines.py
+28-16Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,26 @@
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):
35+
# check valid string
36+
_api.check_in_list([*Line2D._lineStyles, *ls_mapper_r], ls=style)
37+
# go from short hand -> full strings
3638
style = ls_mapper.get(style, style)
39+
# normalize empty style
40+
if style in ('', ' ', 'none'):
41+
style = 'None'
42+
ls = style
43+
else:
44+
ls = 'dashed'
45+
3746
# un-dashed styles
38-
if style in ['solid', 'None']:
47+
if style in ('solid', 'None'):
3948
offset = 0
4049
dashes = None
4150
# dashed styles
42-
elif style in ['dashed', 'dashdot', 'dotted']:
51+
elif style in ('dashed', 'dashdot', 'dotted'):
4352
offset = 0
4453
dashes = tuple(rcParams['lines.{}_pattern'.format(style)])
4554
#
@@ -52,12 +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
60-
76+
if return_ls:
77+
return (offset, dashes), ls
78+
else:
79+
return offset, dashes
6180

6281
def _scale_dashes(offset, dashes, lw):
6382
if not rcParams['lines.scale_dashes']:
@@ -223,6 +242,7 @@ class Line2D(Artist):
223242
'-.': '_draw_dash_dot',
224243
':': '_draw_dotted',
225244
'None': '_draw_nothing',
245+
'none': '_draw_nothing',
226246
' ': '_draw_nothing',
227247
'': '_draw_nothing',
228248
}
@@ -1108,16 +1128,8 @@ def set_linestyle(self, ls):
11081128
11091129
For examples see :doc:`/gallery/lines_bars_and_markers/linestyles`.
11101130
"""
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)
1131+
self._unscaled_dash_pattern, self._linestyle = _get_dash_pattern(ls,
1132+
True)
11211133
self._dash_pattern = _scale_dashes(
11221134
*self._unscaled_dash_pattern, self._linewidth)
11231135
self.stale = True

‎lib/matplotlib/patches.py

Copy file name to clipboardExpand all lines: lib/matplotlib/patches.py
+2-4Lines 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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.