3
$\begingroup$

Assume I have a manipulate with two graphics/plots as outputs. One of my parameters is a locator. How can I select in which of the plots the locator appears?

Let's say I have the output given by this:

Manipulate[{Graphics[Arrow[{{0, 0}, p}], PlotRange -> 10, 
   Axes -> True], 
  Plot[p[[1]]*x + p[[2]], {x, 0, 10}, PlotRange -> 50]}, {p, Locator}]

enter image description here

I want to change the locator from the graphic on the left to the plot on the right.

Here is a screenshot of the code running:

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ @rm-rf oops, I forgot about doing that. Thanks for bringing this to my attention. It failed on 12.0.0 for Linux x86 (64-bit). Works fine for version 12.2 and higher. $\endgroup$ Feb 25, 2022 at 3:17

2 Answers 2

3
$\begingroup$

If you just remove the first Graphics expression, the Locator will show overlaid on the Plot.

Manipulate[Plot[p[[1]]*x + p[[2]], {x, 0, 10}, PlotRange -> 50], {p, Locator}]

If you want to combine both graphical things into one, you can use Show.

Manipulate[
  Show[{Plot[p[[1]]*x + p[[2]], {x, 0, 10}, PlotRange -> 50], 
    Graphics[Arrow[{{0, 0}, p}], PlotRange -> 10, Axes -> True]}], 
  {p, Locator}]

If you want to keep two graphics things, one for manipulating the Locator but the other also showing the Locator, you could do something like this:

Manipulate[
  {Graphics[Arrow[{{0, 0}, p}], PlotRange -> 10, Axes -> True], 
  Show[{Plot[p[[1]]*x + p[[2]], {x, 0, 10}, PlotRange -> 50], 
    Graphics[{Red, PointSize[Large], Point[p]}]}]}, 
  {p, Locator}]
$\endgroup$
3
  • $\begingroup$ Thank you for your time! What about just showing the locator on the plot and not on the graphics? Which is what I am trying to achieve. $\endgroup$ Feb 22, 2022 at 16:57
  • $\begingroup$ @AndrésMorales, you can use a Locator as a Graphics object, and so you can combine a Locator with the Plot using Show. However, the Manipulate gets a bit confused if you just use Locator[p], I think because it's giving precedence to the first Graphics object. So, I used Locator[q] and inserted a q=p; elsewhere in the expression. But, having said all of that, you might want to just create a Dynamic expression rather than a Manipulate. I can try to show that, but can you describe what you want the final output to be? Two synchronized locators overlaid on two different graphical displays? $\endgroup$
    – lericr
    Feb 22, 2022 at 17:20
  • $\begingroup$ Thank you. I want the final output to be a graphic with one locator, and one graphic without a locator (but that uses the info from the locator for calculations). However, I do want to be able to control which graphic has the visual locator. $\endgroup$ Feb 22, 2022 at 19:49
3
$\begingroup$

You could use LocatorPane. Just make sure you wrap everything that needs to be dynamic in Dynamic. Here is a generic example that you can hopefully refine. (You might want the pt to be local, so look into Module or DynamicModule.)

pt = {0, 0};
Grid[{{
  LocatorPane[Dynamic[pt], 
    Dynamic[Graphics[Line[{{0, 0}, pt}], PlotRange -> {{-10, 10}, {-10, 10}}, Frame -> True, Axes -> True, AxesLabel -> {x, y}, AxesOrigin -> {0, 0}, ImageSize -> 300]]], 
  LocatorPane[Dynamic[pt], 
    Dynamic[Plot[pt[[1]]*x + pt[[2]], {x, 0, 10}, PlotRange -> {{-10, 10}, {-50, 50}}, ImageSize -> 300]]]}}]
$\endgroup$
1
  • $\begingroup$ This shows the Locator in both parts, so, just modify to use one LocatorPane on whichever side you want and then a Dynamic[Graphics[...]] in the other (just strip off the LocatorPane). $\endgroup$
    – lericr
    Feb 22, 2022 at 20:30

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.