Skip to content
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

Added a Parameter interpolate_pickpos #19

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions mpldatacursor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
2 changes: 1 addition & 1 deletion mpldatacursor/convenience.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
17 changes: 14 additions & 3 deletions mpldatacursor/datacursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 = []
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 33 additions & 1 deletion mpldatacursor/pick_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``.)
Expand Down Expand Up @@ -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<d:
d = dtmp
i=itmp+1
return dict(x=xorig[i], y=yorig[i])

def collection_props(event):
"""
Expand Down