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

Skip to content

Fix redundent set line props #6175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@ def __call__(self, *args, **kwargs):
ret = self._grab_next_args(*args, **kwargs)
return ret

def set_lineprops(self, line, **kwargs):
assert self.command == 'plot', 'set_lineprops only works with "plot"'
line.set(**kwargs)

def set_patchprops(self, fill_poly, **kwargs):
assert self.command == 'fill', 'set_patchprops only works with "fill"'
fill_poly.set(**kwargs)
Expand Down Expand Up @@ -274,11 +270,10 @@ def _setdefaults(self, defaults, *kwargs):

def _makeline(self, x, y, kw, kwargs):
kw = kw.copy() # Don't modify the original kw.
kwargs = kwargs.copy()
default_dict = self._getdefaults(None, kw, kwargs)
self._setdefaults(default_dict, kw, kwargs)
kw.update(kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pretty extensive notes about keeping kw and kwargs distinct. I can almost guarantee you that this breaks stuff. All my notes about this can be found in the long comments nearby this area of code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of those comments are on the _makefill related code which looks much worse.

In this case the user kwargs were just getting passed to line.set which just farms them back out to line.set_*(...). Now the same thing is happening (through update which does the same dispatching, but does some default cleaning first (which strips the Nones)).

default_dict = self._getdefaults(None, kw)
self._setdefaults(default_dict, kw)
seg = mlines.Line2D(x, y, **kw)
self.set_lineprops(seg, **kwargs)
return seg

def _makefill(self, x, y, kw, kwargs):
Expand Down
56 changes: 47 additions & 9 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@ def __init__(self, xdata, ydata,
if solid_joinstyle is None:
solid_joinstyle = rcParams['lines.solid_joinstyle']

if is_string_like(linestyle):
ds, ls = self._split_drawstyle_linestyle(linestyle)
if ds is not None and drawstyle is not None and ds != drawstyle:
raise ValueError("Inconsistent drawstyle ({0!r}) and "
"linestyle ({1!r})".format(drawstyle,
linestyle)
)
linestyle = ls

if ds is not None:
drawstyle = ds

if drawstyle is None:
drawstyle = 'default'

Expand Down Expand Up @@ -979,6 +991,38 @@ def set_linewidth(self, w):
self.stale = True
self._linewidth = w

def _split_drawstyle_linestyle(self, ls):
'''Split drawstyle from linestyle string

If `ls` is only a drawstyle default to returning a linestyle
of '-'.

Parameters
----------
ls : str
The linestyle to be processed

Returns
-------
ret_ds : str or None
If the linestyle string does not contain a drawstyle prefix
return None, otherwise return it.

ls : str
The linestyle with the drawstyle (if any) stripped.
'''
ret_ds = None
for ds in self.drawStyleKeys: # long names are first in the list
if ls.startswith(ds):
ret_ds = ds
if len(ls) > len(ds):
ls = ls[len(ds):]
else:
ls = '-'
break

return ret_ds, ls

def set_linestyle(self, ls):
"""
Set the linestyle of the line (also accepts drawstyles,
Expand Down Expand Up @@ -1030,15 +1074,9 @@ def set_linestyle(self, ls):
self.set_dashes(ls[1])
self._linestyle = "--"
return

for ds in self.drawStyleKeys: # long names are first in the list
if ls.startswith(ds):
self.set_drawstyle(ds)
if len(ls) > len(ds):
ls = ls[len(ds):]
else:
ls = '-'
break
ds, ls = self._split_drawstyle_linestyle(ls)
if ds is not None:
self.set_drawstyle(ds)

if ls in [' ', '', 'none']:
ls = 'None'
Expand Down
23 changes: 21 additions & 2 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1790,11 +1790,11 @@ def test_boxplot_sym():
def test_boxplot_autorange_whiskers():
x = np.ones(140)
x = np.hstack([0, x, 2])

fig1, ax1 = plt.subplots()
ax1.boxplot([x, x], bootstrap=10000, notch=1)
ax1.set_ylim((-5, 5))

fig2, ax2 = plt.subplots()
ax2.boxplot([x, x], bootstrap=10000, notch=1, autorange=True)
ax2.set_ylim((-5, 5))
Expand Down Expand Up @@ -4237,6 +4237,25 @@ def test_axis_set_tick_params_labelsize_labelcolor():
assert axis_1.yaxis.majorTicks[0]._labelcolor == 'red'


@cleanup
def test_none_kwargs():
fig, ax = plt.subplots()
ln, = ax.plot(range(32), linestyle=None)
assert ln.get_linestyle() == '-'


@cleanup
def test_ls_ds_conflict():
assert_raises(ValueError, plt.plot, range(32),
linestyle='steps-pre:', drawstyle='steps-post')


@cleanup
def test_ls_ds_conflict():
assert_raises(ValueError, plt.plot, range(32),
linestyle='steps-pre:', drawstyle='steps-post')


@image_comparison(baseline_images=['date_timezone_x'], extensions=['png'])
def test_date_timezone_x():
# Tests issue 5575
Expand Down