3
$\begingroup$

My goal is to locate points without using slide bars then average point is dynamically plotted. Could I select and locate a point directly without using the slider bar?(I could locate points using Manipulate function and plot average as below. The problem is that If there are more points(say, hundreds), there should be more sliders, which will take up a lot of space, and it will be difficult to know which slider corresponds to the point concerned.I have searched documnt center and I think that Locate function can be used though)

    Manipulate[
 NumberLinePlot[{{a, b, c, d, e}, {(a + b + c + d + e)/5}}, 
  PlotStyle -> {Black, Red}, PlotRange -> {0, 4}], {{a, 1.5}, 0, 
  4}, {{b, 1}, 0, 4}, {{c, 1.2}, 0, 4}, {{d, 1.3}, 0, 4}, {{e, 1.4}, 
  0, 4}]

enter image description here

$\endgroup$

1 Answer 1

3
$\begingroup$

1. DynamicModule + LocatorPane

You can use LocatorPane and add/remove points using Alt + Click as follows:

DynamicModule[{pts = {{0, 1}, {3, 1}}}, 
 LocatorPane[
  Dynamic[pts, (pts = #; pts[[All, 2]] = 1;) &], 
  Dynamic @ 
   NumberLinePlot[{pts[[All, 1]], {Mean[pts[[All, 1]]]}}, 
     PlotStyle -> (Directive[AbsolutePointSize[10], #] & /@ {Black, Red}),
     ImageSize -> Large, 
     PlotRange -> {0, 4}, 
     ImagePadding -> 30],  
 Appearance -> None,
 LocatorAutoCreate -> True]]

enter image description here

Add the option

Epilog -> {Text[Style[Round[Mean@pts[[All, 1]], .01], 16], 
   Offset[{10, 0}, {Mean@pts[[All, 1]], 2}], {Left, Center}]}

to get

enter image description here

2. Manipulate + Locator

Manipulate[
 NumberLinePlot[{pts[[All, 1]], {Mean[pts[[All, 1]]]}}, 
   Epilog -> {Text[Style[Round[Mean@pts[[All, 1]], .01], 16], 
     Offset[{10, 0}, {Mean@pts[[All, 1]], 2}], {Left, Center}]}, 
   PlotStyle -> (Directive[AbsolutePointSize[10], #] & /@ {Black, Red}), 
   ImageSize -> Large, 
   PlotRange -> {0, 4}, 
   ImagePadding -> 30], 
 {{pts, {{0, 1}, {3, 1}}}, 
   Locator, 
   TrackingFunction -> ((pts = #; pts[[All, 2]] = 1;) &), 
   Appearance -> None, 
   LocatorAutoCreate -> True}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks for your reply. Additional question is.. Could I know why the points have 2-D coordinates as pts = {{0, 1}, {3, 1}}? $\endgroup$
    – Soon
    Oct 10 at 5:22
  • 1
    $\begingroup$ @Soon, we need to use 2D coordinates to work with locators (NumberLinePlot adds the y-coordinates automatically). $\endgroup$
    – kglr
    Oct 10 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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