Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit21e50c5

Browse files
committed
Allow empty linestyle for collections
1 parentf25c2d0 commit21e50c5

File tree

4 files changed

+122
-80
lines changed

4 files changed

+122
-80
lines changed

‎lib/matplotlib/collections.py

Lines 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
defset_linestyle(self,ls):
586586
"""
587-
Set thelinestyle(s) for the collection.
587+
Set theline 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-
ifisinstance(ls,str):
613-
ls=cbook.ls_mapper.get(ls,ls)
625+
ifisinstance(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-
exceptValueError:
619-
dashes= [mlines._get_dash_pattern(x)forxinls]
620-
621-
exceptValueErroraserr:
622-
raiseValueError('Do not know how to convert {!r} to '
623-
'dashes'.format(ls))fromerr
630+
exceptValueError:
631+
dashes= [mlines._get_dash_pattern(x)forxinls]
624632

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

‎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
ifisinstance(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+
ifstylein ('',' ','none'):
47+
style='None'
48+
ls=style
49+
else:# style is a dash tuple
50+
ls='--'
51+
3752
# un-dashed styles
38-
ifstylein['solid','None']:
53+
ifstylein('-','None'):
3954
offset=0
4055
dashes=None
4156
# dashed styles
42-
elifstylein['dashed','dashdot','dotted']:
57+
elifstylein('--','-.',':'):
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
elifisinstance(style,tuple):
4762
offset,dashes=style
4863
ifoffsetisNone:
49-
raiseValueError(f'Unrecognized linestyle:{style!r}')
64+
raiseValueError(f'Unrecognized linestyle:{orig_style!r}')
5065
else:
51-
raiseValueError(f'Unrecognized linestyle:{style!r}')
66+
raiseValueError(f'Unrecognized linestyle:{orig_style!r}')
5267

5368
# normalize offset to be positive and shorter than the dash cycle
5469
ifdashesisnotNone:
70+
try:
71+
ifany(dash<0.0fordashindashes):
72+
raiseValueError(
73+
"All values in the dash list must be non-negative")
74+
iflen(dashes)andnotany(dash>0.0fordashindashes):
75+
raiseValueError(
76+
'At least one value in the dash list must be positive')
77+
exceptTypeError:
78+
raiseValueError(f'Unrecognized linestyle:{orig_style!r}')
5579
dsum=sum(dashes)
5680
ifdsum:
5781
offset%=dsum
5882

59-
returnoffset,dashes
83+
ifreturn_linestyle:
84+
return (offset,dashes),ls
85+
else:
86+
returnoffset,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
defset_linestyle(self,ls):
10811109
"""
1082-
Set thelinestyle of the line.
1110+
Set theline 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-
ifisinstance(ls,str):
1112-
iflsin [' ','','none']:
1113-
ls='None'
1114-
_api.check_in_list([*self._lineStyles,*ls_mapper_r],ls=ls)
1115-
iflsnotinself._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
defset_linestyle(self,ls):
398398
"""
399-
Set the patchlinestyle.
399+
Set the patchline 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
iflsisNone:
423-
ls="solid"
424-
iflsin [' ','','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-
assertpatch.get_linestyle()=='solid'
800+
assertpatch.get_linestyle()=='-'
801801

802802

803803
deftest_default_capstyle():

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp