 # New Pick Method with a simple example

Forgot the previous message, I did a bad manipulation

John,

Sorry for the late answer but I had problems in designing a simple example ( I had the very bad idea to choose letter "l" as the key to press) and also lots of other things to do. Here it is :

···

############################################################################
from pylab import *

def pick(event):
if event.key=='p' and event.inaxes is not None:
ax=event.inaxes
a=ax.pick(event.x,event.y)
a.set_color('g')
a.set_linewidth(2.)
draw()
if event.key=='m' and event.inaxes is not None:
ax=event.inaxes
a=ax.pickBigLine(event.x,event.y)
a.set_color('g')
a.set_linewidth(2.)
draw()
############################################################################
def PlotTwoLines():
plot([0.,0.],[-100.,100.])
plot([2.,2.],[-1.,1.])
connect('key_press_event',pick)
xmin,xmax,ymin,ymax=axis()
axis([xmin-1.,xmax+1.,ymin*1.1,ymax*1.1])
show()
if __name__=='__main__':
PlotTwoLines()
#################################################################################

In this simple program, there is two way for picking a line, the classical pick method associated to the "p" key and PickBigLine method bassociated to the "m" key.
The code just draw two lines a big one and a small one. The classical pick method works prefectly if you do not do a zoom to see the small line. But if you do it to see more accurately this small line then it is impossible to pick the big line unless you use PickBigLine.
Basically, PickBigLine evaluates the orthogonal distance from the selected point to the line by calculating the intersection between the line you want to select
and a line passing through the selected point which is orthogonal.
Here is a modified version of PickBigLine taking into account your remarks and working with Matplotlib 0.82.

--------------------------------------------------------------------------------------------------
def pickBigLine(self, x, y, trans=None):
"""
Return the Line artist under point that is closest to the x, y. if trans
is None, x, and y are in window coords, 0,0 = lower left. Otherwise,
trans is a matplotlib transform that specifies the coordinate system
of x, y.
Calculates the orthogonal distance to the line
Note this algorithm calculates distance to the vertices of the
polygon, so if you want to pick a patch, click on the edge!
"""
if trans is not None:
xywin = trans.xy_tup((x,y))
else:
xywin = x,y

def dist(a):
Cste=1.e6
xdata = a.get_xdata(valid_only = True)
ydata = a.get_ydata(valid_only = True)
# A point is not a line
if len(xdata) == 1: return Cste
xt, yt = a.get_transform().numerix_x_y(xdata, ydata)
xc, yc = xt-xt, yt-yt
if (xc==0.0 and yc == 0.0): return Cste
D = xc*xc + yc*yc
D1 = -(xt-xywin)*yc+(yt-xywin)*xc
D2 = -(yt-xywin)*yc-(xt-xywin)*xc
if D2/D>1.00001 or D2/D<-0.00001: return Cste
return abs(D1/sqrt(D))

artists = self.lines

if not len(artists): return None
ds = [ (dist(a),a) for a in artists]
ds.sort()
return ds
------------------------------------------------------------------------------------

My interests are in ray tracing in geophysics. I am generating a lot of lines
(thousands of) and then I need after zooming to identify trajectories connecting a
source and a receiver . For example when I am picking a line I need to know to
what beam it belongs and also to what ray it coresponds (two integers for
instance) which is something I know when I am plotting the line.
I don't know how to do it with the label property. It is an axes
property not a line2D property.
If you want I can give an example of the use of the tag property I add.

Paul