diff --git a/mpldatacursor/__init__.py b/mpldatacursor/__init__.py index d1a9401..65051e2 100644 --- a/mpldatacursor/__init__.py +++ b/mpldatacursor/__init__.py @@ -21,6 +21,6 @@ """ __version__ = '0.4-dev' -from convenience import datacursor -from datacursor import DataCursor, HighlightingDataCursor +from .convenience import datacursor +from .datacursor import DataCursor, HighlightingDataCursor __all__ = ['datacursor', 'DataCursor', 'HighlightingDataCursor'] diff --git a/mpldatacursor/convenience.py b/mpldatacursor/convenience.py index d32b8eb..9bd6a2f 100644 --- a/mpldatacursor/convenience.py +++ b/mpldatacursor/convenience.py @@ -22,7 +22,7 @@ from matplotlib import _pylab_helpers as pylab_helpers from matplotlib import cbook -from datacursor import DataCursor +from .datacursor import DataCursor def datacursor(artists=None, axes=None, **kwargs): """ diff --git a/mpldatacursor/datacursor.py b/mpldatacursor/datacursor.py index 5c75dc9..baebbc0 100644 --- a/mpldatacursor/datacursor.py +++ b/mpldatacursor/datacursor.py @@ -29,7 +29,7 @@ from matplotlib.collections import PatchCollection, PolyCollection, QuadMesh from matplotlib.lines import Line2D -import pick_info +from . import pick_info class DataCursor(object): """A simple data cursor widget that displays the x,y location of a @@ -41,7 +41,7 @@ class DataCursor(object): arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')) def __init__(self, artists, tolerance=5, formatter=None, point_labels=None, - display='one-per-axes', draggable=False, **kwargs): + display='one-per-axes', draggable=False,interpolate_pickpos=False, **kwargs): """Create the data cursor and connect it to the relevant figure. Parameters @@ -66,9 +66,20 @@ def __init__(self, artists, tolerance=5, formatter=None, point_labels=None, draggable : boolean, optional Controls whether or not the annotation box will be interactively draggable to a new location after being displayed. Default: False. + interpolate_pickpos: boolean defines what kind of line pick function to use. + If set to true the interpolated version will be used. + If false the nearest point to the left will be picked. **kwargs : additional keyword arguments, optional Additional keyword arguments are passed on to annotate. """ + #define which kind of line_props to use. + #line_props <- easy version + #line_props_interpolated <- interpolated version + if (interpolate_pickpos): + self.line_props = pick_info.line_props_interpolated + else: + self.line_props = pick_info.line_props + def filter_artists(artists): """Replace ContourSets with their constituent artists.""" output = [] @@ -150,7 +161,7 @@ def default_func(event): AxesImage : [pick_info.image_props], PathCollection : [pick_info.scatter_props, self._contour_info, pick_info.collection_props], - Line2D : [pick_info.line_props], + Line2D : [self.line_props], LineCollection : [pick_info.collection_props, self._contour_info], PatchCollection : [pick_info.collection_props, diff --git a/mpldatacursor/pick_info.py b/mpldatacursor/pick_info.py index aa0dbab..b7612b0 100644 --- a/mpldatacursor/pick_info.py +++ b/mpldatacursor/pick_info.py @@ -71,7 +71,7 @@ def image_props(event): z = ', '.join('{:0.3g}'.format(item) for item in z) return dict(z=z, i=i, j=j) -def line_props(event): +def line_props_interpolated(event): """ Get information for a pick event on a Line2D artist (as created with ``plot``.) @@ -110,6 +110,38 @@ def line_props(event): x, y = np.array([x0, y0]) + dist_along * vec1 return dict(x=x, y=y) + +def line_props(event): + """ + Get information for a pick event on a Line2D artist (as created with + ``plot``.) + + This will yield x and y values on the nearest pick position of the mouse + + + Parameters + ----------- + event : PickEvent + The pick event to process + + Returns + -------- + props : dict + A dict with keys: x & y + """ + xclick, yclick = event.mouseevent.xdata, event.mouseevent.ydata + #i = event.ind[0] + #Search the nearest point + xorig, yorig = event.artist.get_xydata().T + d = ((xclick-xorig[0])**2 + (yclick-yorig[0])**2)**0.5 + i=0 + + for itmp,(xo,yo) in enumerate(zip(xorig[1:],yorig[1:])): + dtmp = ((xclick-xo)**2 + (yclick-yo)**2)**0.5 + if dtmp