Skip to content

Commit 6376047

Browse files
committed
Merge pull request #3772 from lennart0901/linestyle_#2136_2
API/ENH : Allow both linestyle definition "accents" and dash-patterns as linestyle
2 parents 03eccd0 + 937248d commit 6376047

15 files changed

+763
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Split `matplotlib.cbook.ls_mapper` in two
2+
`````````````````````````````````````````
3+
4+
The `matplotlib.cbook.ls_mapper` dictionary is split into two now to
5+
distinguish between qualified linestyle used by backends and
6+
unqualified ones. `ls_mapper` now maps from the short symbols
7+
(e.g. `"--"`) to qualified names (`"solid"`). The new ls_mapper_r is
8+
the reversed mapping.
9+

doc/users/whats_new/linestyles.rst

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Mostly unified linestyles for Lines, Patches and Collections
2+
````````````````````````````````````````````````````````````
3+
4+
The handling of linestyles for Lines, Patches and Collections has been
5+
unified. Now they all support defining linestyles with short symbols,
6+
like `"--"`, as well as with full names, like `"dashed"`. Also the
7+
definition using a dash pattern (`(0., [3., 3.])`) is supported for all
8+
methods using Lines, Patches or Collections.

lib/matplotlib/cbook.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2194,7 +2194,10 @@ def unmasked_index_ranges(mask, compressed=True):
21942194
(':', 'dotted')]
21952195

21962196
ls_mapper = dict(_linestyles)
2197-
ls_mapper.update([(ls[1], ls[0]) for ls in _linestyles])
2197+
# The ls_mapper maps short codes for line style to their full name used
2198+
# by backends
2199+
# The reverse mapper is for mapping full names to short ones
2200+
ls_mapper_r = dict([(ls[1], ls[0]) for ls in _linestyles])
21982201

21992202

22002203
def align_iterators(func, *iterables):

lib/matplotlib/collections.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -482,27 +482,43 @@ def set_linestyle(self, ls):
482482
"""
483483
Set the linestyle(s) for the collection.
484484
485-
ACCEPTS: ['solid' | 'dashed', 'dashdot', 'dotted' |
486-
(offset, on-off-dash-seq) ]
485+
=========================== =================
486+
linestyle description
487+
=========================== =================
488+
``'-'`` or ``'solid'`` solid line
489+
``'--'`` or ``'dashed'`` dashed line
490+
``'-.'`` or ``'dash_dot'`` dash-dotted line
491+
``':'`` or ``'dotted'`` dotted line
492+
=========================== =================
493+
494+
Alternatively a dash tuple of the following form can be provided::
495+
496+
(offset, onoffseq),
497+
498+
where ``onoffseq`` is an even length tuple of on and off ink
499+
in points.
500+
501+
Parameters
502+
----------
503+
ls : { '-', '--', '-.', ':'} and more see description
504+
The line style.
487505
"""
488506
try:
489507
dashd = backend_bases.GraphicsContextBase.dashd
490508
if cbook.is_string_like(ls):
509+
ls = cbook.ls_mapper.get(ls, ls)
491510
if ls in dashd:
492511
dashes = [dashd[ls]]
493-
elif ls in cbook.ls_mapper:
494-
dashes = [dashd[cbook.ls_mapper[ls]]]
495512
else:
496513
raise ValueError()
497514
elif cbook.iterable(ls):
498515
try:
499516
dashes = []
500517
for x in ls:
501518
if cbook.is_string_like(x):
519+
x = cbook.ls_mapper.get(x, x)
502520
if x in dashd:
503521
dashes.append(dashd[x])
504-
elif x in cbook.ls_mapper:
505-
dashes.append(dashd[cbook.ls_mapper[x]])
506522
else:
507523
raise ValueError()
508524
elif cbook.iterable(x) and len(x) == 2:
@@ -511,7 +527,7 @@ def set_linestyle(self, ls):
511527
raise ValueError()
512528
except ValueError:
513529
if len(ls) == 2:
514-
dashes = ls
530+
dashes = [ls]
515531
else:
516532
raise ValueError()
517533
else:

lib/matplotlib/lines.py

+50-31
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from matplotlib import verbose
1717
from . import artist
1818
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
2020
from .colors import colorConverter
2121
from .path import Path
2222
from .transforms import Bbox, TransformedPath, IdentityTransform
@@ -921,55 +921,74 @@ def set_linewidth(self, w):
921921
"""
922922
self._linewidth = float(w)
923923

924-
def set_linestyle(self, linestyle):
924+
def set_linestyle(self, ls):
925925
"""
926-
Set the linestyle of the line (also accepts drawstyles)
926+
Set the linestyle of the line (also accepts drawstyles,
927+
e.g., ``'steps--'``)
927928
928929
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+
=========================== =================
940941
941942
'steps' is equivalent to 'steps-pre' and is maintained for
942943
backward-compatibility.
943944
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+
944952
.. seealso::
945953
946954
:meth:`set_drawstyle`
947955
To set the drawing style (stepping) of the plot.
948956
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.
953961
"""
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
954969

955970
for ds in self.drawStyleKeys: # long names are first in the list
956-
if linestyle.startswith(ds):
971+
if ls.startswith(ds):
957972
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):]
960975
else:
961-
linestyle = '-'
976+
ls = '-'
962977
break
963978

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
973992

974993
@docstring.dedent_interpd
975994
def set_marker(self, marker):

lib/matplotlib/patches.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,31 @@ def set_linestyle(self, ls):
344344
"""
345345
Set the patch linestyle
346346
347-
ACCEPTS: ['solid' | 'dashed' | 'dashdot' | 'dotted']
347+
=========================== =================
348+
linestyle description
349+
=========================== =================
350+
``'-'`` or ``'solid'`` solid line
351+
``'--'`` or ``'dashed'`` dashed line
352+
``'-.'`` or ``'dash_dot'`` dash-dotted line
353+
``':'`` or ``'dotted'`` dotted line
354+
=========================== =================
355+
356+
Alternatively a dash tuple of the following form can be provided::
357+
358+
(offset, onoffseq),
359+
360+
where ``onoffseq`` is an even length tuple of on and off ink
361+
in points.
362+
363+
Parameters
364+
----------
365+
ls : { '-', '--', '-.', ':'} and more see description
366+
The line style.
348367
"""
349368
if ls is None:
350369
ls = "solid"
370+
371+
ls = cbook.ls_mapper.get(ls, ls)
351372
self._linestyle = ls
352373

353374
def set_ls(self, ls):

0 commit comments

Comments
 (0)