Restrict Locator to a certain graphic inside Manipulate



I would like to restrict the Locator inside Manipulate to a certain graphic. For example if I display two graphs, I would like to be able to click on the right one and use this information in other places (for example in the title and the left graph). By design, Manipulate assigns the Locator to the first graphic object that it displays (see under Details and Options). How can this be overcome?

Here is an example snippet:

 sin =
  Plot[Sin[x], {x, -5, 5}, ImageSize -> Medium, 
   Epilog -> {Red, PointSize[Large], Point[p]}];
 cos = Plot[Cos[x], {x, -5, 5}, 
   PlotLabel -> "I would like to have the locator on this graph!", 
   ImageSize -> Medium, Epilog -> {Green, PointSize[Large], Point[p]}];
 Column[{Style[StringForm["Title `1`", p], Large],
   Row[{sin, cos}]}, Center],
 {{p, {0, 0}}, {-5, -5}, {5, 5}, ControlType -> Locator}]

example output

I know this can be done using Dynamic, but then I loose some features of manipulate, such as "Paste Snapshoot", "Make Bookmark" and SaveDefinitions


Posted 2012-05-24T19:56:51.250

Reputation: 13 114



Edit: I am replacing my first attempt with another. The locator-point in the right graph controls the information in the title as well as the point in the left graph.

The point on the right could be made to appear like a locator by replacing Point with an appropriate-looking graphics object.

  Column[{Style[StringForm["Title `1`", Dynamic@pt], Large], 
       Row[{Plot[Cos[x], {x, -5, 5}, 
       Epilog -> {PointSize[Large], Point[Dynamic[{First[pt], Cos[First[pt]]}]]}, 
       ImageSize -> 300], 
       LocatorPane[Dynamic[pt], Plot[Sin[x], {x, -5, 5}, 
         Epilog -> {PointSize[Large], 
         Point[Dynamic[{First[pt], Sin[First[pt]]}]]}, 
         ImageSize -> 300, 
         PlotLabel -> "I would like to have the locator on this graph!"], 
       Appearance -> None]}]}, Center], {{pt, {0, 1}}, None}]

locator on right


Normally one confines a Locator to a particular region using optional parameters in LocatorPane:

LocatorPane instructions

However, it is not necessary to do this above because the LocatorPane sits in a single cell in the table. The locator cannot exceed the bounds of that cell.


Posted 2012-05-24T19:56:51.250

Reputation: 15 949

I switched the sin and cos graphs, but the intent should be clear. – DavidC – 2012-05-24T21:52:09.983

+1 Thanks! Do you perhaps have an idea, why the title is messed up, when you do "Paste snapshot"? (And why it works if I pout the Dynamic around Style instead of pt? – Ajasja – 2012-05-24T21:55:28.637

@Ajasja I'm not sure why you want to use "paste snapshot". What are you trying to paste, and where? – DavidC – 2012-05-24T22:02:49.487

The paste snapshot is available when you click the + in the upper right corner of the manipulate pane. If I try to evaluate the new cell then the title does not seam to get the value. I want to use this to bookmark interesting parameter combinations as shown here under "Bookmarking Combinations of Parameter Values"

– Ajasja – 2012-05-24T22:10:42.690

Apparently, the rendering of the PlotLabel depends on activation of Style. Dynamic apparently ensures that it is up to date. – DavidC – 2012-05-24T22:57:49.213


You could use Overlay:

 Column[{Style[StringForm["Title `1`", Dynamic[p]], Large], Overlay[{
      Plot[Cos[x], {x, -5, 5}, 
       PlotLabel -> 
        "I would like to have the locator on this graph!",
       ImageSize -> Large, 
       Epilog -> {Green, PointSize[Large], Point[p]}, 
       PlotRegion -> {{0.5, 1}, {0, 1}}],
      Plot[Sin[x], {x, -5, 5}, ImageSize -> Large, 
       Epilog -> {Red, PointSize[Large], Point[p]},
       PlotRegion -> {{0, .5}, {0, 1}}, 
       PlotLabel -> "Not on this graph!"]
     }, All, 1]}, Center],
 {{p, {0, 0}}, {-5, -1}, {5, 1}, ControlType -> Locator}]



I added explicit Dynamic around objects depending on p to prevent sporadic loss of mouse tracking, and to hopefully speed up the response. The documentation for Manipulate states:

In the form {u,Locator}, the value of u is a list giving x and y coordinates. The coordinates refer either to the first graphic in expr, or range from 0 to 1 in each direction across expr.

Here I'm relying on the coordinates of the first graphic in the Overlay expression.

What I did to arrange the graphics horizontally is simply to set their PlotRegion option to leave either the left or right half of the surrounding space empty. In the Overlay, they then fill the two halves to form what looks like a row.


Posted 2012-05-24T19:56:51.250

Reputation: 93 191

Due to the way my other data is laid out I would like the locator be on the right. This works by setting Overlay[{sin, cos}], although I have no idea why. Also with Overlay manipulate seems to respond slower than with LocatorPane. – Ajasja – 2012-05-24T21:57:44.000

@Ajasja Yes, you're right about the speed. Honestly, I don't like Manipulate. – Jens – 2012-05-24T22:03:23.127

Yes, sadly I had a lot of problems as well. Although with a bit of tweaking and hacking and workarounds I get there eventually. I hope this will improve in mma 9 :) – Ajasja – 2012-05-24T22:07:12.943

There is a problem with this solution: the locator moves even if I click in the "Title" section, so it is not really restricted to the right plot region. – shrx – 2016-02-08T12:28:27.363

@shrx The locator is restricted to the right plot region. I just tried it. It does move to the edge of the region if you click as you describe, but it never leaves that region. – Jens – 2016-02-08T15:25:07.357

Yes, I thought it should not react to clicks outside its region. – shrx – 2016-02-09T12:27:28.420