I had been using a custom function (written originally by Jae-Joon and
modified a little by me...quite a long time back now) that was working to
allow point picking of markers, but *not* the line connecting them.
However, I've now discovered with the help of this list that the function I
am using has the disadvantage that if there are more than 100 data points,
I can't get the correct index for the picked marker (turned out not to be a
mpl bug: https://github.com/matplotlib/matplotlib/issues/3124).
So I can just use the default pick event, but then the user can pick
anywhere on the connecting line, which is meaningless in this use--so I
don't want them to be able to pick on the connecting line.
My goal is to have a custom function that will serve *both* purposes:
allow picking the markers only, not the line, of a set of data of any
length while returning the correct index of that marker/data point. But
the code in the custom function is mostly above my head, was written for
mpl 0.99 or so, and I don't know how to modify it to get both purposes
achieved.
I attach the current sample again, with the problematic custom picker
function, "contains_points()". Thanks for any suggestions or help.
Che
import matplotlib.pyplot as plt
import numpy as np
def contains_points(line, mouseevent):
line.pickradius = 5
# Make sure we have data to plot
if line._invalidy or line._invalidx:
line.recache()
if len(line._xy)==0: return False,{}
# Convert points to pixels
if line._transformed_path is None:
line._transform_path()
path, affine = line._transformed_path.get_transformed_points_and_affine()
path = affine.transform_path(path)
xy = path.vertices
xt = xy[:, 0]
yt = xy[:, 1]
pixels = line.figure.dpi/72. * line.pickradius
d = (xt-mouseevent.x)**2 + (yt-mouseevent.y)**2
ind, = np.nonzero(np.less_equal(d, pixels**2))
print 'index is: ', str(ind)
return len(ind)>0,dict(ind=ind)
dates = [735079.1214674653, 735079.5, 735079.55647688662, 735079.60561398149, 735079.60901608795, 735079.61007837963, 735079.61141004635, 735079.61222394672, 735079.61267262732, 735079.61740547454, 735079.61793575226, 735079.61845732643, 735079.61902608792, 735079.68499270838, 735079.68542109954, 735079.68880315975, 735079.68926655094, 735079.68966354162, 735079.69596565969, 735079.701868125, 735079.70749983797, 735079.70960563654, 735079.71045478014, 735079.71102318284, 735079.71184613428, 735079.71230732638, 735079.71268356487, 735079.71303708339, 735079.71386268514, 735079.71445497684, 735079.7153444445, 735079.71684614581, 735079.7183792477, 735079.71955292823, 735079.72024780093, 735079.72192891198, 735079.72248410876, 735079.72560098383, 735079.72600572917, 735079.72638543986, 735079.72990056709, 735079.73168333329, 735079.73226472223, 735079.73661531252, 735079.74144714116, 735079.74522572919, 735079.7468240856, 735079.74791210645, 735079.97846979171, 735079.98271479167, 735079.98447646992, 735080.12350728014, 735080.14091008098, 735080.55523804401, 735080.56125733792, 735080.63896374998, 735080.64021659724, 735080.64103072917, 735080.66268849536, 735080.7048011343, 735080.79961501155, 735080.86546409724, 735080.99817599542, 735081.0204026273, 735081.02133151621, 735081.02613285882, 735081.0271783449, 735081.0335956713, 735081.04115539347, 735081.04800446762, 735081.05008083337, 735081.05534546298, 735081.05918304401, 735081.06037712959, 735081.06172269676, 735081.06712594908, 735081.08196986106, 735081.62065618054, 735081.8688092361, 735081.86910491902, 735081.86998233793, 735081.99360535876, 735081.99586380785, 735082.00806468748, 735082.0098228819, 735082.01533834497, 735082.01674383099, 735082.02936186339, 735082.02976707171, 735082.03022675926, 735082.03069490741, 735082.03097482643, 735082.03125312505, 735082.03604334488, 735082.03765619209, 735082.05257208331, 735082.100969919, 735082.1024177199, 735082.10387097218, 735082.1388320023, 735082.33333333337, 735082.53595807869, 735082.55833702546, 735082.56112760422, 735083.00588994217, 735083.007571875, 735113.50807679398, 735113.58693798608, 735114.03848809027, 735114.04119372682, 735115.5, 735119.8093059028, 735120.03856688656, 735253.07588778937, 735256.95615627314, 735258.69064364582, 735258.69268998841, 735258.69969299773, 735258.72694862273, 735259.62416826386, 735259.62484111113, 735259.7361906945, 735259.75713817135, 735259.99948642356, 735260.00099298614, 735260.00174059032, 735260.00592855329, 735260.00680168986, 735260.00748263893, 735260.00827880786, 735260.00860694447, 735260.00958392362, 735260.02752453706, 735260.06593625003, 735260.07891475689, 735260.07907957176, 735260.07935114589, 735260.62010422454, 735260.62646800932, 735260.62662517361, 735260.66605687502, 735260.67675605323, 735260.68322124996, 735260.68342049769, 735260.68355356483, 735260.68561972224, 735260.68846386578, 735269.73599923612, 735285.60832390049, 735302.07172445604, 735304.07737268519, 735304.39253472222, 735306.02329861117, 735307.09005787037, 735309.60149305558]
values = [0.0019444444444444444, 1.0, 0.0011111111111111111, 0.0011111111111111111, 0.0030555555555555557, 0.00083333333333333339, 0.0011111111111111111, 0.00055555555555555556, 0.00055555555555555556, 0.0011111111111111111, 0.00055555555555555556, 0.00027777777777777778, 0.00055555555555555556, 0.00083333333333333339, 0.00055555555555555556, 0.00055555555555555556, 0.00055555555555555556, 0.0027777777777777779, 0.00055555555555555556, 0.00083333333333333339, 0.00055555555555555556, 0.00027777777777777778, 0.00055555555555555556, 0.00055555555555555556, 0.00027777777777777778, 0.00055555555555555556, 0.00027777777777777778, 0.00083333333333333339, 0.00055555555555555556, 0.00055555555555555556, 0.00055555555555555556, 0.00027777777777777778, 0.00027777777777777778, 0.00055555555555555556, 0.00055555555555555556, 0.00027777777777777778, 0.00027777777777777778, 0.00055555555555555556, 0.00055555555555555556, 0.00027777777777777778, 0.00055555555555555556, 0.00055555555555555556, 0.00027777777777777778, 0.00027777777777777778, 0.00027777777777777778, 0.00027777777777777778, 0.00055555555555555556, 0.00027777777777777778, 0.00055555555555555556, 0.00055555555555555556, 0.0036111111111111109, 0.00083333333333333339, 0.0013888888888888889, 0.00027777777777777778, 0.00083333333333333339, 0.0022222222222222222, 0.00055555555555555556, 0.0063888888888888893, 0.00055555555555555556, 0.00055555555555555556, 0.00055555555555555556, 0.00055555555555555556, 0.00055555555555555556, 0.00083333333333333339, 0.00055555555555555556, 0.00055555555555555556, 0.00055555555555555556, 0.00055555555555555556, 0.00027777777777777778, 0.00027777777777777778, 0.00055555555555555556, 0.00055555555555555556, 0.00083333333333333339, 0.00027777777777777778, 0.00055555555555555556, 0.00083333333333333339, 0.0016666666666666668, 0.00027777777777777778, 0.00055555555555555556, 0.0016666666666666668, 0.00055555555555555556, 0.00027777777777777778, 0.00055555555555555556, 0.00083333333333333339, 0.00083333333333333339, 0.00055555555555555556, 0.00027777777777777778, 0.00027777777777777778, 0.00027777777777777778, 0.00027777777777777778, 0.00055555555555555556, 0.0011111111111111111, 0.00055555555555555556, 0.00055555555555555556, 0.0013888888888888889, 0.0013888888888888889, 0.00027777777777777778, 0.0036111111111111109, 0.0016666666666666668, 0.00055555555555555556, 0.00027777777777777778, 0.00027777777777777778, 0.00055555555555555556, 0.00055555555555555556, 0.0025000000000000001, 0.017500000000000002, 0.0027777777777777779, 0.00083333333333333339, 0.00055555555555555556, 0.0027777777777777779, 1.0, 0.0013888888888888889, 0.0013888888888888889, 0.046388888888888889, 0.023333333333333334, 0.0027777777777777779, 0.0011111111111111111, 0.0011111111111111111, 0.00083333333333333339, 0.0013888888888888889, 0.00083333333333333339, 0.013888888888888888, 0.0063888888888888893, 0.00083333333333333339, 0.0019444444444444444, 0.0033333333333333335, 0.0016666666666666668, 0.0019444444444444444, 0.0025000000000000001, 0.00083333333333333339, 0.0011111111111111111, 0.00055555555555555556, 0.0011111111111111111, 0.0027777777777777779, 0.0013888888888888889, 0.0011111111111111111, 0.0011111111111111111, 0.0013888888888888889, 0.0011111111111111111, 0.00083333333333333339, 0.0011111111111111111, 0.00083333333333333339, 0.00055555555555555556, 0.00055555555555555556, 0.00083333333333333339, 0.0027777777777777779, 0.00083333333333333339, 0.00083333333333333339, 0.00055555555555555556, 0.0025000000000000001, 0.008611111111111111, 0.026111111111111113, 0.013055555555555556, 0.010833333333333334, 2.338888888888889]
truncation_number = 101
truncated_dates = dates[:truncation_number]
truncated_durations = values[:truncation_number]
plt.plot(truncated_dates,truncated_durations,'o-',picker=contains_points)
plt.show()
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users