def __pick(self, x, y, trans=None, among=None):
<EOL>if trans is not None:<EOL><INDENT>xywin = trans.transform_point((x,y))<EOL><DEDENT>else:<EOL><INDENT>xywin = x,y<EOL><DEDENT>def dist_points(p1, p2):<EOL><INDENT>'<STR_LIT>'<EOL>x1, y1 = p1<EOL>x2, y2 = p2<EOL>return math.sqrt((x1-x2)**<NUM_LIT:2>+(y1-y2)**<NUM_LIT:2>)<EOL><DEDENT>def dist_x_y(p1, x, y):<EOL><INDENT>'<STR_LIT>'<EOL>x1, y1 = p1<EOL>return min(np.sqrt((x-x1)**<NUM_LIT:2>+(y-y1)**<NUM_LIT:2>))<EOL><DEDENT>def dist(a):<EOL><INDENT>if isinstance(a, Text):<EOL><INDENT>bbox = a.get_window_extent()<EOL>l,b,w,h = bbox.bounds<EOL>verts = (l,b), (l,b+h), (l+w,b+h), (l+w, b)<EOL>xt, yt = list(zip(*verts))<EOL><DEDENT>elif isinstance(a, Patch):<EOL><INDENT>path = a.get_path()<EOL>tverts = a.get_transform().transform_path(path)<EOL>xt, yt = list(zip(*tverts))<EOL><DEDENT>elif isinstance(a, mlines.Line2D):<EOL><INDENT>xdata = a.get_xdata(orig=False)<EOL>ydata = a.get_ydata(orig=False)<EOL>xt, yt = a.get_transform().numerix_x_y(xdata, ydata)<EOL><DEDENT>return dist_x_y(xywin, np.asarray(xt), np.asarray(yt))<EOL><DEDENT>artists = self.lines + self.patches + self.texts<EOL>if callable(among):<EOL><INDENT>artists = list(filter(test, artists))<EOL><DEDENT>elif iterable(among):<EOL><INDENT>amongd = dict([(k,<NUM_LIT:1>) for k in among])<EOL>artists = [a for a in artists if a in amongd]<EOL><DEDENT>elif among is None:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if not len(artists): return None<EOL>ds = [ (dist(a),a) for a in artists]<EOL>ds.sort()<EOL>return ds[<NUM_LIT:0>][<NUM_LIT:1>]<EOL>
Return the 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
:class:`~matplotlib.transforms.Transform` that specifies the
coordinate system of *x*, *y*.

The selection of artists from amongst which the pick function
finds an artist can be narrowed using the optional keyword
argument *among*. If provided, this should be either a sequence
of permitted artists or a function taking an artist as its
argument and returning a true value if and only if that artist
can be selected.

Note this algorithm calculates distance to the vertices of the
polygon, so if you want to pick a patch, click on the edge!