3
$\begingroup$

I need to plot a listplot with its $(x,y)$ coordinate when my mouse go over a value of the function.

I want the simplest way to do it.

I have read topic using the notations "#", I just learnt how it works but if you use it I would like to have some explanations (that also could be an example for me to understand well how it works).

$\endgroup$
  • $\begingroup$ Try right-clicking the plot and use "Get Coordinates? tool $\endgroup$ – Shadowray May 30 '17 at 12:39
  • $\begingroup$ The problem is it is referenced to the origin of the picture. When i resize it it doesn't correspond. What's more it returns values where my mouse is and i just want a value on the function to have the exact result $\endgroup$ – StarBucK May 30 '17 at 12:40
4
$\begingroup$

Data

Fake data, you use your own.

data = Transpose[{Range[10], RandomReal[1, 10]}];

Simple solution

Auxiliary function to add Tooltip

addXYToolTip[{x_, y_}] := Tooltip[{x, y}, {x, y}]

The ListPlot

ListPlot[
 Map[addXYToolTip, data]
 ]

Mathematica graphics

Better solution

data2 = Transpose[Sort /@ RandomReal[1, {2, 20}]]
addXYToolTip2[{x_, y_}, {n_}] := Tooltip[
  {x, y}
  , Framed@Grid[
    {
     {"n: ", n},
     {"x: ", x},
     {"y: ", y}
     },
    Alignment -> Left]
  ]

ListPlot[
 MapIndexed[addXYToolTip2, data2]
 ]

Mathematica graphics

$\endgroup$
3
$\begingroup$

In:

xss = Table[{x, Sin[x]}, {x, 0, 2 Pi, 0.05}];
mouseover[c_] := Mouseover[c, Text[c, c + {-0.2, 0.2}]]
plot[xss_] := xss // Map[mouseover] // ListPlot
plot[xss]

Out: enter image description here

$\endgroup$
  • $\begingroup$ interesting!!!! $\endgroup$ – partida May 31 '17 at 0:00
3
$\begingroup$

You can use Nearest + Dynamic as follows:

(* sample data *)
data=Table[{x,Sin[x]},{x,0,Pi,.1}];

(* nearest function *)
nf=Nearest[data];

ListPlot[
    data,
    Epilog -> Dynamic @ DynamicModule[
        {
            pt=nf[MousePosition[{"Graphics",Graphics},{0,0}]],
            scaled=MousePosition[{"GraphicsScaled",Graphics},None]
        },

        If[scaled===None,
            {},
            {
                Text[pt[[1]], pt[[1]], {1.5 Sign[scaled[[1]]-.5], 0}, Background->White],
                AbsolutePointSize[7], Point[pt], White, AbsolutePointSize[5], Point[pt]
            }
        ]
    ]
]

enter image description here

As long as the mouse pointer is in the graphic, the nearest data point will get highlighted.

$\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.