Skip to content

Zooming to a point changes a picked point's index for data longer than 100 points #3124

Closed
@cmpython

Description

@cmpython

If you have a list longer than 100 data points, and zoom to the final point and click on it (while using point picking), the index number of that point will be reset to a much lower number, depending on how tightly you zoom.

In the example below, using 155 data points from my data, the data starts out truncated to the first 101 data points. Run the program (tested; works with Python 2.7 and Matplotlib 1.3.1) and click the last data point. What will be printed in the shell is, "index is: [100]". Now zoom into that point very tightly, and click on it again. Now what will be printed is, "index is: [1]".

You can change the truncation_number near the end of the program and if you set it to be 100 or less, this error will not occur.

Small runnable sample code follows:

#---------------------------------------------------------------------------

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]

#Anything over 101 seems to cause the problem
truncation_number = 101

truncated_dates = dates[:truncation_number]
truncated_durations = values[:truncation_number]

print 'List truncated to ' + str(truncation_number) + ' points'

plt.plot(truncated_dates,truncated_durations,'o-',picker=contains_points)

plt.show()

TAC - edited to add code-markup

Activity

tacaswell

tacaswell commented on Jun 7, 2014

@tacaswell
Member

This is likely do to optimizations that drop points that we can't see, but I can see why this is a major problem for UIs.

@joferkington Have you seen this working on mpldatacursor?

efiring

efiring commented on Jun 7, 2014

@efiring
Member

The number 100 comes from the threshold for using subslices when zooming. The contains method is doing the right thing here; the problem is that the user code is not taking this subslicing into account. Yes, the path is truncated; that's an intended optimization. If you want to find the indices into the original data, then you need to look not at the plotted path, but at that original data. See Line2D.contains(); it is returning the indices of the points in the original data.

In summary, I don't see any bug here in mpl.

cmpython

cmpython commented on Jun 7, 2014

@cmpython
Author

On Sat, Jun 7, 2014 at 5:52 PM, Eric Firing notifications@github.com
wrote:

The number 100 comes from the threshold for using subslices when zooming.
The contains method is doing the right thing here; the problem is that
the user code is not taking this subslicing into account. Yes, the path is
truncated; that's an intended optimization. If you want to find the indices
into the original data, then you need to look not at the plotted path,
but at that original data. See Lines.contains(); it is returning the
indices of the points in the original data.

In summary, I don't see any bug here in mpl.

That's good it is not an mpl bug, but I am trying and don't know how/where
I should modify my code to use Lines.contains(). Could you give more
detail about what changes I could make to the code? Thank you.

Che


Reply to this email directly or view it on GitHub
#3124 (comment)
.

tacaswell

tacaswell commented on Jun 7, 2014

@tacaswell
Member

Sorry, I didn't read your code carefully enough. The soultion is actually much simpler,

Picking actually fires off a PickEvent which has the attribute ind which is I think what you want (with out you having to figure it out your self).

efiring

efiring commented on Jun 7, 2014

@efiring
Member

In the matplotlib examples directory, see event_handling/pick_event_demo.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @efiring@tacaswell@cmpython

        Issue actions

          Zooming to a point changes a picked point's index for data longer than 100 points · Issue #3124 · matplotlib/matplotlib