4
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Oct 20, 2014 at 14:15
  • $\begingroup$ I would like any point on the curve. $\endgroup$
    – faysou
    Oct 20, 2014 at 16:06
  • $\begingroup$ Thank you for the answers, each one is interesting, the one of Fred Simons is closest to the question. $\endgroup$
    – faysou
    Oct 21, 2014 at 7:13

4 Answers 4

2
$\begingroup$

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]]]
$\endgroup$
4
4
$\begingroup$

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"]]]
$\endgroup$
3
$\begingroup$
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]

enter image description here

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]]

enter image description here

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
   ]
  ]
 ]

enter image description here

$\endgroup$
0
$\begingroup$

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]
            ]
        ]
    ];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.