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

Skip to content

Commit 722f405

Browse files
committed
Line2D._path obeys drawstyle.
For stepping drawstyles, `Line2D.draw` used to recompute a path at drawing time, because its `_path` attribute always assumed a linear drawstyle. Instead, directly compute the correct path. Also fixes #6447 (`Line2D.contains` did not take drawstyle into account) because that code relied on proximity of the mouse event with the underlying path.
1 parent 77c23f8 commit 722f405

File tree

1 file changed

+13
-38
lines changed

1 file changed

+13
-38
lines changed

lib/matplotlib/lines.py

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,22 @@ class Line2D(Artist):
237237
'steps': '_draw_steps_pre',
238238
}
239239

240+
# drawStyles should now be deprecated.
240241
drawStyles = {}
241242
drawStyles.update(_drawStyles_l)
242243
drawStyles.update(_drawStyles_s)
243244
# Need a list ordered with long names first:
244245
drawStyleKeys = (list(six.iterkeys(_drawStyles_l)) +
245246
list(six.iterkeys(_drawStyles_s)))
246247

248+
_drawstyle_conv = {
249+
'default': lambda x, y: (x, y),
250+
'steps': pts_to_prestep,
251+
'steps-pre': pts_to_prestep,
252+
'steps-mid': pts_to_midstep,
253+
'steps-post': pts_to_poststep
254+
}
255+
247256
# Referenced here to maintain API. These are defined in
248257
# MarkerStyle
249258
markers = MarkerStyle.markers
@@ -470,8 +479,7 @@ def contains(self, mouseevent):
470479
# application has set the error flags such that an exception is raised
471480
# on overflow, we temporarily set the appropriate error flags here and
472481
# set them back when we are finished.
473-
olderrflags = np.seterr(all='ignore')
474-
try:
482+
with np.errstate(all='ignore'):
475483
# Check for collision
476484
if self._linestyle in ['None', None]:
477485
# If no line, return the nearby point(s)
@@ -480,20 +488,9 @@ def contains(self, mouseevent):
480488
else:
481489
# If line, return the nearby segment(s)
482490
ind = segment_hits(mouseevent.x, mouseevent.y, xt, yt, pixels)
483-
finally:
484-
np.seterr(**olderrflags)
485491

486492
ind += self.ind_offset
487493

488-
# Debugging message
489-
if False and self._label != '':
490-
print("Checking line", self._label,
491-
"at", mouseevent.x, mouseevent.y)
492-
print('xt', xt)
493-
print('yt', yt)
494-
#print 'dx,dy', (xt-mouseevent.x)**2., (yt-mouseevent.y)**2.
495-
print('ind', ind)
496-
497494
# Return the point(s) within radius
498495
return len(ind) > 0, dict(ind=ind)
499496

@@ -691,7 +688,8 @@ def recache(self, always=False):
691688
interpolation_steps = self._path._interpolation_steps
692689
else:
693690
interpolation_steps = 1
694-
self._path = Path(self._xy, None, interpolation_steps)
691+
xy = self._drawstyle_conv[self._drawstyle](*self._xy.T)
692+
self._path = Path(np.asarray(xy).T, None, interpolation_steps)
695693
self._transformed_path = None
696694
self._invalidx = False
697695
self._invalidy = False
@@ -764,8 +762,6 @@ def draw(self, renderer):
764762
tpath, affine = transf_path.get_transformed_path_and_affine()
765763
if len(tpath.vertices):
766764
self._lineFunc = getattr(self, funcname)
767-
funcname = self.drawStyles.get(self._drawstyle, '_draw_lines')
768-
drawFunc = getattr(self, funcname)
769765
gc = renderer.new_gc()
770766
self._set_gc_clip(gc)
771767

@@ -788,7 +784,7 @@ def draw(self, renderer):
788784
if self.get_sketch_params() is not None:
789785
gc.set_sketch_params(*self.get_sketch_params())
790786

791-
drawFunc(renderer, gc, tpath, affine.frozen())
787+
self._draw_lines(renderer, gc, tpath, affine.frozen())
792788
gc.restore()
793789

794790
if self._marker and self._markersize > 0:
@@ -1234,27 +1230,6 @@ def set_dashes(self, seq):
12341230
def _draw_lines(self, renderer, gc, path, trans):
12351231
self._lineFunc(renderer, gc, path, trans)
12361232

1237-
def _draw_steps_pre(self, renderer, gc, path, trans):
1238-
steps = np.vstack(pts_to_prestep(*self._xy.T)).T
1239-
1240-
path = Path(steps)
1241-
path = path.transformed(self.get_transform())
1242-
self._lineFunc(renderer, gc, path, IdentityTransform())
1243-
1244-
def _draw_steps_post(self, renderer, gc, path, trans):
1245-
steps = np.vstack(pts_to_poststep(*self._xy.T)).T
1246-
1247-
path = Path(steps)
1248-
path = path.transformed(self.get_transform())
1249-
self._lineFunc(renderer, gc, path, IdentityTransform())
1250-
1251-
def _draw_steps_mid(self, renderer, gc, path, trans):
1252-
steps = np.vstack(pts_to_midstep(*self._xy.T)).T
1253-
1254-
path = Path(steps)
1255-
path = path.transformed(self.get_transform())
1256-
self._lineFunc(renderer, gc, path, IdentityTransform())
1257-
12581233
def _draw_solid(self, renderer, gc, path, trans):
12591234
gc.set_linestyle('solid')
12601235
renderer.draw_path(gc, path, trans)

0 commit comments

Comments
 (0)