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 21e50c5

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

File tree

4 files changed

+122
-80
lines changed
Filter options

4 files changed

+122
-80
lines changed

‎lib/matplotlib/collections.py

Copy file name to clipboardExpand all lines: lib/matplotlib/collections.py
+41-33Lines changed: 41 additions & 33 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
@@ -584,43 +584,51 @@ def set_linewidth(self, lw):
584584

585585
def set_linestyle(self, ls):
586586
"""
587-
Set the linestyle(s) for the collection.
587+
Set the line style(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+
Parameters
590+
----------
591+
ls : str or tuple or list thereof
592+
The line style. Possible values:
597593
598-
Alternatively a dash tuple of the following form can be provided::
594+
- A string:
599595
600-
(offset, onoffseq),
596+
========================================== =================
597+
linestyle description
598+
========================================== =================
599+
``'-'`` or ``'solid'`` solid line
600+
``'--'`` or ``'dashed'`` dashed line
601+
``'-.'`` or ``'dashdot'`` dash-dotted line
602+
``':'`` or ``'dotted'`` dotted line
603+
``'none'``, ``'None'``, ``' '``, or ``''`` draw nothing
604+
========================================== =================
601605
602-
where ``onoffseq`` is an even length tuple of on and off ink in points.
606+
- Alternatively a dash tuple of the following form can be
607+
provided::
603608
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.
609+
(offset, onoffseq)
610+
611+
where ``onoffseq`` is an even length tuple of on and off ink
612+
in points.
613+
614+
If a single value is provided, this applies to all objects in the
615+
collection. A list can be provided to set different line styles to
616+
different objects.
617+
618+
For examples see :doc:`/gallery/lines_bars_and_markers/linestyles`.
619+
620+
The ``'dashed'``, ``'dashdot'``, and ''`dotted'`` line styles are
621+
controlled by :rc:`lines.dashed_pattern`,
622+
:rc:`lines.dashdot_pattern`, and :rc:`lines.dotted_pattern`,
623+
respectively.
610624
"""
611-
try:
612-
if isinstance(ls, str):
613-
ls = cbook.ls_mapper.get(ls, ls)
625+
if isinstance(ls, str):
626+
dashes = [mlines._get_dash_pattern(ls)]
627+
else:
628+
try:
614629
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
630+
except ValueError:
631+
dashes = [mlines._get_dash_pattern(x) for x in ls]
624632

625633
# get the list of raw 'unscaled' dash patterns
626634
self._us_linestyles = dashes

‎lib/matplotlib/lines.py

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

Copy file name to clipboardExpand all lines: lib/matplotlib/patches.py
+31-22Lines 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. See also :meth:`set_dashes`.
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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_patches.py
+1-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.