2
$\begingroup$

In the following LogLogPlot, I'd like to have the Dynamic[pt] color change from Purple to Blue - when above the Purple horizontal line - and change from Purple to Red when below the line. So far, Directive[Purple] initializes the color:

DynamicModule[{pt = {-9., -55.2}}, LocatorPane[Dynamic[pt],
LogLogPlot[1/x^3, {x, 10^-12, 10^8},
Epilog -> {
  {PointSize[Large], Directive[Purple], 
   Point[Dynamic[{First[pt], 1/Exp[First@pt]^3 // Log}]]},
  {Inset[Style["\[LongDash]\[LongDash]\[LongDash]\[LongDash]", Bold, Purple], {-9, 
   26.5}]}
          },
          AxesOrigin -> {0, 0}, AspectRatio -> 1, ImageSize -> {520, 400}]
]]

enter image description here

However, attempts at using If[...] logic, or the following Which[...] intervals logic, returns the Error message: "Which is not a graphics directive":

Directive[Which[
         Dynamic[pt] == {Interval[{.0001, 10^-12}], 
                         Interval[{10^-9, 10^33}]}, Blue,
         Dynamic[pt] == {Interval[{.0001, .001}], Interval[{10^-11, 10^-9}]},
                         Purple,
         Dynamic[pt] == {Interval[{.001, 10^8}], Interval[{10^-11, 10^-25}]},
                         Red]]

Also, perhaps I should post this as a separate question, but it would be really tight if the Locator itself were constrained to the x axis.

$\endgroup$
2
  • $\begingroup$ btw, is the original one laggy for you too, or is it only me? $\endgroup$ – Kuba Nov 22 '18 at 23:02
  • $\begingroup$ Nope, neither the original, or @kglr solution, or now my embellished version have any noticeable lag - running on a 2017 MacBook. That is something on my mind though - since I'm working on a loaded up Demonstration Project wondering how well it will work on other systems. $\endgroup$ – ehounder Nov 23 '18 at 1:32
2
$\begingroup$

(1) You can use the second argument of Dynamic to restrict the locator to the horizontal axis, and (2) you can use Dynamic@Directive[If[First[pt] <= -9, Purple, Red]] to change the color of the point based on the locator position:

DynamicModule[{pt = {-9., -55.2}}, 
 LocatorPane[Dynamic[pt, (pt = {#[[1]], -55.2}) &], 
  LogLogPlot[1/x^3, {x, 10^-12, 10^8}, 
   Epilog -> {{PointSize[Large], 
       Dynamic@Directive[If[First[pt] <= -9, Purple, Red]], 
       Point[Dynamic[{First[pt], 1/Exp[First@pt]^3 // Log}]]}, {Inset[
       Style["\[LongDash]\[LongDash]\[LongDash]\[LongDash]", Bold, 
        Purple], {-9, 26.5}]}}, AxesOrigin -> {0, 0}, 
   AspectRatio -> 1, ImageSize -> {520, 400}]]]

enter image description here

$\endgroup$

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.