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

Skip to content

Commit 4f3b5cc

Browse files
committed
Merge pull request #6175 from tacaswell/fix_redundent_set_line_props
Fix redundent set line props
2 parents b946dd0 + 3cada62 commit 4f3b5cc

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,6 @@ def __call__(self, *args, **kwargs):
184184
ret = self._grab_next_args(*args, **kwargs)
185185
return ret
186186

187-
def set_lineprops(self, line, **kwargs):
188-
assert self.command == 'plot', 'set_lineprops only works with "plot"'
189-
line.set(**kwargs)
190-
191187
def set_patchprops(self, fill_poly, **kwargs):
192188
assert self.command == 'fill', 'set_patchprops only works with "fill"'
193189
fill_poly.set(**kwargs)
@@ -274,11 +270,10 @@ def _setdefaults(self, defaults, *kwargs):
274270

275271
def _makeline(self, x, y, kw, kwargs):
276272
kw = kw.copy() # Don't modify the original kw.
277-
kwargs = kwargs.copy()
278-
default_dict = self._getdefaults(None, kw, kwargs)
279-
self._setdefaults(default_dict, kw, kwargs)
273+
kw.update(kwargs)
274+
default_dict = self._getdefaults(None, kw)
275+
self._setdefaults(default_dict, kw)
280276
seg = mlines.Line2D(x, y, **kw)
281-
self.set_lineprops(seg, **kwargs)
282277
return seg
283278

284279
def _makefill(self, x, y, kw, kwargs):

lib/matplotlib/lines.py

Lines changed: 47 additions & 9 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

@@ -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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4261,6 +4261,25 @@ def test_axis_set_tick_params_labelsize_labelcolor():
42614261
assert axis_1.yaxis.majorTicks[0]._labelcolor == 'red'
42624262

42634263

4264+
@cleanup
4265+
def test_none_kwargs():
4266+
fig, ax = plt.subplots()
4267+
ln, = ax.plot(range(32), linestyle=None)
4268+
assert ln.get_linestyle() == '-'
4269+
4270+
4271+
@cleanup
4272+
def test_ls_ds_conflict():
4273+
assert_raises(ValueError, plt.plot, range(32),
4274+
linestyle='steps-pre:', drawstyle='steps-post')
4275+
4276+
4277+
@cleanup
4278+
def test_ls_ds_conflict():
4279+
assert_raises(ValueError, plt.plot, range(32),
4280+
linestyle='steps-pre:', drawstyle='steps-post')
4281+
4282+
42644283
@image_comparison(baseline_images=['date_timezone_x'], extensions=['png'])
42654284
def test_date_timezone_x():
42664285
# Tests issue 5575

0 commit comments

Comments
 (0)