6
$\begingroup$

Using Tooltip, is it possible to display the numeric value of the curve I've plotted using Plot and PolarPlot?

For example, is it possible to display the numeric y value of Sin[x] (or any other curve) with Tooltip?

$\endgroup$
4

2 Answers 2

4
$\begingroup$

I thought I did something like the following before, but maybe it was not for this site:

DynamicModule[{y0},
 EventHandler[
  Plot[Tooltip[Sin[x], Dynamic[y0]], {x, 0, 2 Pi}],
  "MouseMoved" :> (y0 = Sin@First@MousePosition["Graphics"]),
  ]]

Update, per comment. Note that MousePosition["Graphics"] returns the coordinates of the mouse, not of a point on the graphics. It is triggered when the mouse is near the graphics, so it gives only a very rough approximation to the function value. One can take the x-coordinate of the mouse as given and calculate the y-coordinate from the function. If the function is complicated, one might not want to copy the code into both arguments of Tooltip; I use With below so one needs only to type the function expression once. (There's a red syntax warning on the x inside Dynamic, because Dynamic is HoldFirst and Plot effectively uses Block to assign x a value; but it's just what we need here, since Plot finishes evaluating before Dynamic reaches the front end.)

With[{f = Sin[x]},
 Plot[
  Tooltip[f,
   Dynamic[f /. x -> First@MousePosition["Graphics"]]],
  {x, 0, 2 Pi}]
 ]

Bells & whistles:

With[{f = Sin[x]},
 DynamicModule[{x1, y1},
  EventHandler[
   Plot[
    f,
    {x, 0, 2 Pi},
    MaxRecursion -> 0,
    Epilog -> {Red, Dynamic@Tooltip[Point[{x1, y1}], {x1, y1}]},
    GridLines -> {
      Dynamic@If[NumericQ[x1], {x1}, None],
      Dynamic@If[NumericQ[y1], {y1}, None]}
    ],
   "MouseMoved" :> (
     x1 = First@MousePosition["Graphics", {x1, 0}];;
     y1 = f /. x -> x1)],
  Initialization :> (
    x1 = Pi; y1 = f /. x -> Pi)]]
$\endgroup$
2
  • 6
    $\begingroup$ for minimal functionality this is enough Plot[Tooltip[Sin[x], Dynamic@MousePosition["Graphics"]], {x, 0, 2 Pi}] $\endgroup$
    – Kuba
    Jul 14, 2016 at 18:38
  • $\begingroup$ @Kuba D'oh! Thanks! (You can answer with that, if you like. But I'm tempted to close as dupe....) $\endgroup$
    – Michael E2
    Jul 14, 2016 at 18:53
3
$\begingroup$

Potential way to achieve this:

pl = Plot[Sin[x], {x, 0, 10}];
vals = Table[Tooltip[{x, Sin[x]}, Sin[x]], {x, 0, 10, .05}];
lp = ListPlot[vals, PlotStyle -> None];
Show[{pl, lp}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.