1
$\begingroup$

Is it possible to make a plotted curve interactive in the sense that when hovering over it a line is drawn from the "hovered" point to one (or both) of the axes. I did do my homework, I just can't figure it out.

Edit: Let's say I plot this curve:

Manipulate[Plot[Sin[a x],{x,0,Pi}],{a,1,5}]

Whenever the mouse hovers over a point on the curve a line is drawn from that point to the x-axis.

$\endgroup$

migrated from stackoverflow.com Jul 10 '12 at 21:01

This question came from our site for professional and enthusiast programmers.

8
$\begingroup$

Something like this?

DynamicModule[{mp, x, y, fun},
 fun[x_, a_] := Sin[a x];
 Manipulate[
  mp = MousePosition["Graphics"];
  If[mp === None,
   epi = {},
   {x, y} = mp;
   epi =
    If[
     Abs[y - fun[x, a]] < 0.1,
     Line[{{x, 0}, {x, fun[x, a]}}],
     {}
     ]
   ];
  Plot[fun[x, a], {x, 0, Pi}, Epilog -> epi]
  , {a, 1, 5}
  ]
 ]

Mathematica graphics

$\endgroup$
5
$\begingroup$
 Manipulate[Plot[Sin[a x], {x, 0, 10}, 
 Epilog -> {Red, Thick, Line[Dynamic[{{First[pt], 0}, 
         {First[pt],Sin[a First[pt]]}}]]}], 
 {a, 1, 5}, 
 {{pt, {0, 0}}, ControlType -> Locator, Appearance -> None, AutoAction -> True}]

enter image description here

 Manipulate[
 Plot[Sin[a x], {x, 0, 10}, 
 Epilog -> {Red, Thick, Line[Dynamic@{{First[pt], 0}, {First[pt], Sin[a First[pt]]}}], 
  Line[Dynamic@{{0, Sin[a First[pt]]}, {First[pt], Sin[a First[pt]]}}], 
  Black, PointSize[.03], Point[Dynamic@{First[pt], Sin[a First[pt]]}]}], 
 {a, 1, 5}, {{pt, {0, 0}}, ControlType -> Locator, Appearance -> None, 
 AutoAction -> True}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy