From 722f405ae740c9f9cb8b40e5a0897b4f78feed72 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 29 May 2016 10:42:40 -0700 Subject: [PATCH 1/2] 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. --- lib/matplotlib/lines.py | 51 +++++++++++------------------------------ 1 file changed, 13 insertions(+), 38 deletions(-) diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index 55762a084f5b..a3a34c7c0e04 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -237,6 +237,7 @@ class Line2D(Artist): 'steps': '_draw_steps_pre', } + # drawStyles should now be deprecated. drawStyles = {} drawStyles.update(_drawStyles_l) drawStyles.update(_drawStyles_s) @@ -244,6 +245,14 @@ class Line2D(Artist): drawStyleKeys = (list(six.iterkeys(_drawStyles_l)) + list(six.iterkeys(_drawStyles_s))) + _drawstyle_conv = { + 'default': lambda x, y: (x, y), + 'steps': pts_to_prestep, + 'steps-pre': pts_to_prestep, + 'steps-mid': pts_to_midstep, + 'steps-post': pts_to_poststep + } + # Referenced here to maintain API. These are defined in # MarkerStyle markers = MarkerStyle.markers @@ -470,8 +479,7 @@ def contains(self, mouseevent): # application has set the error flags such that an exception is raised # on overflow, we temporarily set the appropriate error flags here and # set them back when we are finished. - olderrflags = np.seterr(all='ignore') - try: + with np.errstate(all='ignore'): # Check for collision if self._linestyle in ['None', None]: # If no line, return the nearby point(s) @@ -480,20 +488,9 @@ def contains(self, mouseevent): else: # If line, return the nearby segment(s) ind = segment_hits(mouseevent.x, mouseevent.y, xt, yt, pixels) - finally: - np.seterr(**olderrflags) ind += self.ind_offset - # Debugging message - if False and self._label != '': - print("Checking line", self._label, - "at", mouseevent.x, mouseevent.y) - print('xt', xt) - print('yt', yt) - #print 'dx,dy', (xt-mouseevent.x)**2., (yt-mouseevent.y)**2. - print('ind', ind) - # Return the point(s) within radius return len(ind) > 0, dict(ind=ind) @@ -691,7 +688,8 @@ def recache(self, always=False): interpolation_steps = self._path._interpolation_steps else: interpolation_steps = 1 - self._path = Path(self._xy, None, interpolation_steps) + xy = self._drawstyle_conv[self._drawstyle](*self._xy.T) + self._path = Path(np.asarray(xy).T, None, interpolation_steps) self._transformed_path = None self._invalidx = False self._invalidy = False @@ -764,8 +762,6 @@ def draw(self, renderer): tpath, affine = transf_path.get_transformed_path_and_affine() if len(tpath.vertices): self._lineFunc = getattr(self, funcname) - funcname = self.drawStyles.get(self._drawstyle, '_draw_lines') - drawFunc = getattr(self, funcname) gc = renderer.new_gc() self._set_gc_clip(gc) @@ -788,7 +784,7 @@ def draw(self, renderer): if self.get_sketch_params() is not None: gc.set_sketch_params(*self.get_sketch_params()) - drawFunc(renderer, gc, tpath, affine.frozen()) + self._draw_lines(renderer, gc, tpath, affine.frozen()) gc.restore() if self._marker and self._markersize > 0: @@ -1234,27 +1230,6 @@ def set_dashes(self, seq): def _draw_lines(self, renderer, gc, path, trans): self._lineFunc(renderer, gc, path, trans) - def _draw_steps_pre(self, renderer, gc, path, trans): - steps = np.vstack(pts_to_prestep(*self._xy.T)).T - - path = Path(steps) - path = path.transformed(self.get_transform()) - self._lineFunc(renderer, gc, path, IdentityTransform()) - - def _draw_steps_post(self, renderer, gc, path, trans): - steps = np.vstack(pts_to_poststep(*self._xy.T)).T - - path = Path(steps) - path = path.transformed(self.get_transform()) - self._lineFunc(renderer, gc, path, IdentityTransform()) - - def _draw_steps_mid(self, renderer, gc, path, trans): - steps = np.vstack(pts_to_midstep(*self._xy.T)).T - - path = Path(steps) - path = path.transformed(self.get_transform()) - self._lineFunc(renderer, gc, path, IdentityTransform()) - def _draw_solid(self, renderer, gc, path, trans): gc.set_linestyle('solid') renderer.draw_path(gc, path, trans) From b8fe72cd6641dce4f874d72cc2664cb9e0335bc1 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 29 May 2016 13:13:32 -0700 Subject: [PATCH 2/2] Fix and use STEP_LOOKUP_MAP, for consistency. --- lib/matplotlib/cbook.py | 10 ++++++---- lib/matplotlib/lines.py | 12 ++---------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index a9d17813f0c8..89ba9c11ea45 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -2403,12 +2403,14 @@ def pts_to_midstep(x, *args): # convert 2D array back to tuple return tuple(steps) -STEP_LOOKUP_MAP = {'pre': pts_to_prestep, +STEP_LOOKUP_MAP = {'default': lambda x, y: (x, y), + 'pre': pts_to_prestep, 'post': pts_to_poststep, 'mid': pts_to_midstep, - 'step-pre': pts_to_prestep, - 'step-post': pts_to_poststep, - 'step-mid': pts_to_midstep} + 'steps': pts_to_prestep, + 'steps-pre': pts_to_prestep, + 'steps-post': pts_to_poststep, + 'steps-mid': pts_to_midstep} def index_of(y): diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index a3a34c7c0e04..285d68db6f16 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -17,7 +17,7 @@ from . import artist, colors as mcolors from .artist import Artist from .cbook import (iterable, is_string_like, is_numlike, ls_mapper_r, - pts_to_prestep, pts_to_poststep, pts_to_midstep) + STEP_LOOKUP_MAP) from .path import Path from .transforms import Bbox, TransformedPath, IdentityTransform @@ -245,14 +245,6 @@ class Line2D(Artist): drawStyleKeys = (list(six.iterkeys(_drawStyles_l)) + list(six.iterkeys(_drawStyles_s))) - _drawstyle_conv = { - 'default': lambda x, y: (x, y), - 'steps': pts_to_prestep, - 'steps-pre': pts_to_prestep, - 'steps-mid': pts_to_midstep, - 'steps-post': pts_to_poststep - } - # Referenced here to maintain API. These are defined in # MarkerStyle markers = MarkerStyle.markers @@ -688,7 +680,7 @@ def recache(self, always=False): interpolation_steps = self._path._interpolation_steps else: interpolation_steps = 1 - xy = self._drawstyle_conv[self._drawstyle](*self._xy.T) + xy = STEP_LOOKUP_MAP[self._drawstyle](*self._xy.T) self._path = Path(np.asarray(xy).T, None, interpolation_steps) self._transformed_path = None self._invalidx = False pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy