6

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?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
no16
  • 75
  • 2
  • Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Jul 14 '16 at 17:53
  • Possible duplicate: http://mathematica.stackexchange.com/questions/17545/display-function-value-on-hover -- seems to be the same idea as @user6014's answer below. – Michael E2 Jul 14 '16 at 17:57
  • Also related: http://mathematica.stackexchange.com/questions/34672/how-to-i-display-the-value-of-my-plot-function – Michael E2 Jul 14 '16 at 18:06
  • See the "Get coordinates tool" in the docs – Jens Jul 14 '16 at 18:10

2 Answers2

4

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)]]
Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • 6
    for minimal functionality this is enough Plot[Tooltip[Sin[x], Dynamic@MousePosition["Graphics"]], {x, 0, 2 Pi}] – Kuba Jul 14 '16 at 18:38
  • @Kuba D'oh! Thanks! (You can answer with that, if you like. But I'm tempted to close as dupe....) – Michael E2 Jul 14 '16 at 18:53
3

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}]
ktm
  • 4,242
  • 20
  • 28