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
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 communityThis 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]]]
ListPlot
interpolates each coordinate as a function of the list index.
$\endgroup$
Oct 20, 2014 at 14:06
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]
]
]
];
Tooltip
or coordinates of the original interpolation points? Also note that the interpolation method whichListPlot
uses differs from the default interpolation method inInterpolation
. $\endgroup$