11
$\begingroup$

I am trying to create a user interface where I can click on certain points in a set of data and save them into a list that can be used later in my code. The problem is it is currently sluggish. Is there a way to speed this up?

Also how can I make it so I can unclick a point I have previously click I remove it from the list?

testdata = Array[{#, 1 + #^2*RandomReal[]} &, 100];
clicks = {testdata[[1]], testdata[[-1]]};

Dynamic[ListPlot[{Button[Tooltip@#, clicks = Join[clicks, {#}]] & /@ 
testdata, Sort[clicks, #1[[1]] < #2[[1]] &]}, {ImageSize -> 700, 
 PlotRange -> Automatic}]]

plot

$\endgroup$
10
$\begingroup$

Whole ListPlot is recalculated at each click because clicks value has changed, you can isolate Dynamic parts of graphics in Epilog to avoid that.

Using EventHandlers there will allow you to drop clicked points.

testdata = Array[{#, 1 + #^2*RandomReal[]} &, 100];
clicks = {testdata[[1]], testdata[[-1]]};


ListPlot[     
   Button[Tooltip @ #, AppendTo[clicks, #]] & /@ testdata,
   ImageSize -> 700, 
   PlotRange -> Automatic, 
   Epilog    -> {
     Dynamic[ 
       EventHandler[
         {AbsolutePointSize@12, Orange, Point@#}, 
         "MouseClicked" :> {clicks = DeleteCases[clicks, #]}
       ] & /@ clicks
     ]}
]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Wow thank you for the quick response. Long time lurker first time poster and it went better than I could have expected! Much smoother code now thanks again for the help. $\endgroup$ – Nicklar Feb 23 '16 at 21:19
  • 1
    $\begingroup$ @Nicklar I'm glad it fits your needs. Feel free to ask if anything is not clear. p.s. you can accept answers that are completely solving your problems, please take a tour. $\endgroup$ – Kuba Feb 27 '16 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.