Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42654d0

Browse files
committed
MNT: normalize line/draw style in Line2D.__init__
Because the drawstyle can be set via both the drawstyle and via linestyle do some checking / normalize in the init. Pulled the code that is shared between the `__init__` and `set_linestyle` into a new private method. Reverts minor change to order of operations
1 parent a2b90a8 commit 42654d0

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

lib/matplotlib/lines.py

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,18 @@ def __init__(self, xdata, ydata,
314314
if solid_joinstyle is None:
315315
solid_joinstyle = rcParams['lines.solid_joinstyle']
316316

317+
if is_string_like(linestyle):
318+
ds, ls = self._split_drawstyle_linestyle(linestyle)
319+
if ds is not None and drawstyle is not None and ds != drawstyle:
320+
raise ValueError("Inconsistent drawstyle ({0!r}) and "
321+
"linestyle ({1!r})".format(drawstyle,
322+
linestyle)
323+
)
324+
linestyle = ls
325+
326+
if ds is not None:
327+
drawstyle = ds
328+
317329
if drawstyle is None:
318330
drawstyle = 'default'
319331

@@ -332,8 +344,8 @@ def __init__(self, xdata, ydata,
332344

333345
self._dashSeq = None
334346

335-
self.set_drawstyle(drawstyle)
336347
self.set_linestyle(linestyle)
348+
self.set_drawstyle(drawstyle)
337349
self.set_linewidth(linewidth)
338350

339351
self._color = None
@@ -979,6 +991,38 @@ def set_linewidth(self, w):
979991
self.stale = True
980992
self._linewidth = w
981993

994+
def _split_drawstyle_linestyle(self, ls):
995+
'''Split drawstyle from linestyle string
996+
997+
If `ls` is only a drawstyle default to returning a linestyle
998+
of '-'.
999+
1000+
Parameters
1001+
----------
1002+
ls : str
1003+
The linestyle to be processed
1004+
1005+
Returns
1006+
-------
1007+
ret_ds : str or None
1008+
If the linestyle string does not contain a drawstyle prefix
1009+
return None, otherwise return it.
1010+
1011+
ls : str
1012+
The linestyle with the drawstyle (if any) stripped.
1013+
'''
1014+
ret_ds = None
1015+
for ds in self.drawStyleKeys: # long names are first in the list
1016+
if ls.startswith(ds):
1017+
ret_ds = ds
1018+
if len(ls) > len(ds):
1019+
ls = ls[len(ds):]
1020+
else:
1021+
ls = '-'
1022+
break
1023+
1024+
return ret_ds, ls
1025+
9821026
def set_linestyle(self, ls):
9831027
"""
9841028
Set the linestyle of the line (also accepts drawstyles,
@@ -1030,15 +1074,9 @@ def set_linestyle(self, ls):
10301074
self.set_dashes(ls[1])
10311075
self._linestyle = "--"
10321076
return
1033-
1034-
for ds in self.drawStyleKeys: # long names are first in the list
1035-
if ls.startswith(ds):
1036-
self.set_drawstyle(ds)
1037-
if len(ls) > len(ds):
1038-
ls = ls[len(ds):]
1039-
else:
1040-
ls = '-'
1041-
break
1077+
ds, ls = self._split_drawstyle_linestyle(ls)
1078+
if ds is not None:
1079+
self.set_drawstyle(ds)
10421080

10431081
if ls in [' ', '', 'none']:
10441082
ls = 'None'

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4250,6 +4250,12 @@ def test_ls_ds_conflict():
42504250
linestyle='steps-pre:', drawstyle='steps-post')
42514251

42524252

4253+
@cleanup
4254+
def test_ls_ds_conflict():
4255+
assert_raises(ValueError, plt.plot, range(32),
4256+
linestyle='steps-pre:', drawstyle='steps-post')
4257+
4258+
42534259
@image_comparison(baseline_images=['date_timezone_x'], extensions=['png'])
42544260
def test_date_timezone_x():
42554261
# Tests issue 5575

0 commit comments

Comments
 (0)