2
$\begingroup$

This is driving me nuts. Consider this Manipulate command

points = RandomReal[{0, 2}, {10, 2}];
Manipulate[
 p1 = Graphics[{Red, PointSize -> Large, Point@points}, 
   PlotRange -> 3, Axes -> True];
 p2 = ContourPlot[Norm[{par1*x1, x2}], {x1, 0, 2}, {x2, 0, 2}, 
   ImageSize -> s];
 Grid[{{p1}, {Show[p2, p1]}, {loc}}, Alignment -> Left]
 , {par1, 0, 1}
 , {par2, 0, 1}
 , {{loc, {1, 1}}, Locator}
 , {s, 100, 300}
 , ControlPlacement -> Left
 ]

enter image description here

As far as I have always done something with Locator, it appears in the first graphic you display in Manipulate environments. If you just run my code, it does what I expected. But, when I change the size of the second graphic p2 around the size of p1, the Locator jumps to p2.

enter image description here

Why??? How would you prevent this from happening?

$\endgroup$
2
$\begingroup$

I don't know what to expect from Locator in this case so let's force it to be where we want to:

points = RandomReal[{0, 2}, {10, 2}];

Manipulate[
 p1 = Graphics[{Red, PointSize -> Large, Point@points}, 
   Epilog -> {Dynamic@Locator@Dynamic@loc}, PlotRange -> 3, Axes -> True];

 p2 = ContourPlot[Norm[{par1*x1, x2}], {x1, 0, 2}, {x2, 0, 2}, 
   ImageSize -> s];

 Grid[{{p1}, {Show[p2, p1]}, {Dynamic@loc}}, Alignment -> Left]
 , 
 {par1, 0, 1}, {par2, 0, 1}, 
 {{loc, {1, 1}}, None}, {s, 100, 300}, 
 ControlPlacement -> Left
]

Notice the Epilog which is a convenient way to be sure that the Locator won't show in Show.

enter image description here

$\endgroup$
3
$\begingroup$

Maybe I don't fully understand the problem, but I think Kuba's code is overly complex, and I can't see why the following simpler implementation of isn't a better approach.

SeedRandom[42];
pts = RandomReal[{0, 2}, {10, 2}];
Manipulate[
  p1 =
   Graphics[{Red, PointSize -> Large, Point@pts},
     PlotRange -> {{0, 2}, {0, 2}},
     Axes -> True,
     ImageSize -> s1];
  p2 =
    ContourPlot[
      Norm[{par1*x1, x2}], {x1, 0, 2}, {x2, 0, 2},
      ImageSize -> s2];
  Column[{LocatorPane[Dynamic @ loc, p1], Show[p2, p1]},
   Alignment -> Left],
  {{loc, {1, 1}}, None},
  {par1, 0, 1},
  {s1, 100, 300},
  {s2, 100, 300}]

demo

The only changes I made that are relevant to your problem are the use of Column, LocatorPane, and the control specification {{loc, {1, 1}}, None}. The other changes are only to get rid on irrelevant code and to make the graphics more compact for display on this site.

The reason for the locator jumping from one graphics object to another in you code is that the locator in seeking the largest graphics object in the content pane.

$\endgroup$
  • $\begingroup$ This is the simplest way to go about it: just wrap the image/plot of interest in LocatorPane[], and set the corresponding ControlType to none. $\endgroup$ – J. M. is away Mar 30 '18 at 0:15

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.