# Tooltip with ListLinePlot and interpolation

Is it possible to get the coordinates of any point on a graph using Tooltip, ListLinePlot and interpolation ?

For example on this simple example

ListLinePlot[{{1,2,3},{1,2,3}}//Transpose,InterpolationOrder->2]


Thank you

• Your question is vague: do you need the coordinates of any point on the line in a Tooltip or coordinates of the original interpolation points? Also note that the interpolation method which ListPlot uses differs from the default interpolation method in Interpolation. Commented Oct 20, 2014 at 14:15
• I would like any point on the curve. Commented Oct 20, 2014 at 16:06
• Thank you for the answers, each one is interesting, the one of Fred Simons is closest to the question. Commented Oct 21, 2014 at 7:13

This will return coordinates on the interpolated line

data = {{1, 2, 3}, {1, 2, 3}} // Transpose;

f = Interpolation[data,
InterpolationOrder -> 2];

Tooltip[Plot[f[x],
{x, Min[data[[All, 1]]], Max[data[[All, 1]]]}],
Dynamic[{#, f[#]} &@
MousePosition["Graphics"][[1]]]]


For multiple data sets

data = {
{{1, 1}, {2, 2}, {3, 3}},
{{1, 2}, {2, 1}, {3, 4}},
{{1, 4}, {2, 1}, {3, 2}}};

{min, max} =
#[Flatten[data, 1][[All, 1]]] & /@
{Min, Max};

funcs[x_] = #[x] & /@
(Interpolation[#, InterpolationOrder -> 2] & /@
data);

Tooltip[
Plot[
Evaluate[funcs[x]],
{x, min, max}],
Dynamic[
{mpx, mpy} = MousePosition["Graphics"];
fy = Nearest[funcs[mpx], mpy][[1]];
Style[{mpx, fy}, 14]]]

• Commented Oct 20, 2014 at 14:06
• @Bob would you know how to display a different tooltip for different curves ? Commented Oct 25, 2014 at 17:25
• See addition above Commented Oct 27, 2014 at 14:46
• That's perfect thank you very much. Commented Oct 29, 2014 at 11:52

The following command produces a graph, of which the coordinates of the points are shown in a tooltip when the mouse is over it.

 Tooltip[ListLinePlot[{{1, 2, 3}, {1, 2, 3}} // Transpose,
InterpolationOrder -> 2], Dynamic[MousePosition["Graphics"]]]

ListLinePlot[Tooltip @ Cases[
ListLinePlot[Transpose[{{1, 2, 3}, {1, 2, 3}}],
Mesh -> All,
InterpolationOrder -> 2][[1, 2]],
{a_Real, b_Real}, -1],
Filling -> Bottom,
GridLines -> Automatic,
Mesh -> All]


Similarly for InterpolationOrder -> 0

ListLinePlot[Tooltip @ Sort @ Cases[
ListLinePlot[Transpose[{{1, 2, 3}, {1, 2, 3}}],
Mesh -> All,
InterpolationOrder -> 0][[1, 2]],
{a_Real, b_Real}, -1],
Filling -> Bottom,
GridLines -> Automatic,
Mesh -> All,
MeshStyle -> Directive[PointSize[0.02], Red]]


Update

If you want to specify the number of mesh points a more complicated approach is required

With[{points = 10},
Show[
#,
ListPlot[
Tooltip @ Take[Cases[#[[1, 2]], {a_Real, b_Real}, -1], -points],
PlotStyle -> Directive[PointSize[0.02], Red]]
] &
[
ListLinePlot[Transpose[{{1, 2, 3}, {1, 2, 3}}],
Frame -> True,
GridLines -> Automatic,
Mesh -> points - 1,
InterpolationOrder -> 2
]
]
]


I've generalised the answer of Bob Hanlon as I used it in many parts of my code. The function takes as argument: a list of functions, a range for the x axis, an optional callback function, optional options of Plot, optional options of Tooltip.

Example

MouseOverPlot[
{#^2&,#^3&},{0,5},
"Callback"->
Function[{xValue,functionValues,functionIndex},
Column[
{
"x: "~~ToString@xValue,
{"x^2","x^3"}[[functionIndex]]~~": "~~ToString@functionValues[[functionIndex]]
}
]
]
]


Code

(*callback[xValue,functionValues,functionIndex]*)
Options[MouseOverPlot] = Join[{"Callback"->({#1,#2[[#3]]}&)},Options@Plot,Options@Tooltip];
MouseOverPlot[functions_,plotRange_,opts:OptionsPattern[]]:=
DynamicModule[{mousePosition,xValue,yValue,functionValues,funcs,functionIndex,callback,newPlotRange,xPlot},

funcs[x_] = #[x]& /@ functions // Quiet;
callback = OptionValue@"Callback";
newPlotRange = Prepend[plotRange,xPlot];

With[{newPlotRange=newPlotRange,options=FilterRules[{opts},Options@Plot]},
Tooltip[
Plot[
Evaluate[funcs[xPlot]]
,
newPlotRange
,
options
]
,
Dynamic[
mousePosition = MousePosition["Graphics"];

If[mousePosition =!= None,

{xValue,yValue}=mousePosition;
functionValues = funcs[xValue];
functionIndex=Nearest[functionValues->Automatic,yValue][[1]];

callback[xValue,functionValues,functionIndex]
,
""
]
]
,
FilterRules[{opts},Options@Tooltip]
]
]
];