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
?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityUsing 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
?
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)]]
Plot[Tooltip[Sin[x], Dynamic@MousePosition["Graphics"]], {x, 0, 2 Pi}]
$\endgroup$
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}]