2

I plotted a data
ListPlot[{{1,2,3},{4,5,6}}]. When I point the mouse over point {1}, it should appear at the end of the tooltip and the corresponding data point {4} should be highlighted and its value should be displayed. How can I achieve this?

Edit: Thank you @glS for the answer. I would like to highlight the data just like before but in the TwoAxisListplot as given in any answer here. How can I achieve this

no-one
  • 1,243
  • 9
  • 14
  • 2
    Have you taken a look at Tooltip in the documentation? – MarcoB Jul 27 '16 at 16:35
  • Yes I did, But tooltip enables us just to locate individual points, but what I want is in addition to locating the original data like point 1 along with point 4 highlighted and displaying the value of 4 by that point – no-one Jul 27 '16 at 16:44
  • 1
    I think you really want BubbleChart! – Jens Aug 03 '16 at 06:44

2 Answers2

6

An example using DynamicWrapper:

f = DynamicModule[{},
    DynamicWrapper[Dynamic@Point[{##}], 
     If[CurrentValue["MouseOver"], col = Red; pt = {##}; ps = 0.04;, 
      col = Black; ps = 0.02; pt = {};]]] &;
dat = {{1, 2, 3}, {4, 5, 6}};
Graphics[{PointSize[0.02], f @@@ Transpose[dat], 
  Dynamic@{Opacity[0.5], col, PointSize[ps], Point[pt], Opacity[1], 
    If[Length[pt] == 2, Text[pt, pt, {-2, -2}]]}}, Frame -> True, 
 PlotRange -> {{0, 7}, {0, 7}}]

enter image description here

Update

I post this to address OP comment (displaying value of second axis). It could be made a lot cleaner but I leave it in case it can be suitaly adapted for desired outcome.

fticks = {#, 1.5 #} & /@ Range[0, 7];
Graphics[{PointSize[0.02], f @@@ Transpose[dat], 
  Dynamic@{Opacity[0.5], col, PointSize[ps], Point[pt], Opacity[1], 
    If[Length[pt] == 2, Text[pt, pt, {-2, -2}]], 
    If[Length[pt] == 2, {Arrow[{pt, {7, pt[[2]]}}], 
      Text[1.5 Last@pt, {(7 + pt[[1]])/2, Last@pt}, {0, -1}]}]}}, 
 Frame -> True, 
 FrameTicks -> {{Automatic, 
    Dynamic@If [Length[pt] == 2, 
      fticks~Join~{{Last@pt, 1.5 Last@pt, {0.02, 0.02}, 
         Directive[Red, Thick]}}, fticks]}, {Automatic, None}}, 
 PlotRange -> {{0, 7}, {0, 7}}]

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
  • thanks for the answer, but it is not in the form of two axis plot. Your code is just pointing out points{1,4},{2,5},{3,6} where I want is a two axis plot as given in the link in the question – no-one Aug 02 '16 at 14:45
5

Most probably not the simplest solution, but here is a way using Graphics to plot the data and EventHandler and Tooltip to do the dynamic highlighting:

listPlotWithHighlight[data_] := With[{
   colors = ColorData["Rainbow"] /@ (1/Range@Length@data),
   points = MapIndexed[{First@#2, #1} &] /@ data,
   initPointSizes = 0.02 ConstantArray[1, Length@First@data]
   },
  DynamicModule[{hlPointIndex = 0, pointSizes = initPointSizes},
   Graphics[{
     Dynamic@MapIndexed[{
          colors[[First@#2]],
          MapIndexed[
            Function[{point, index},
             {PointSize@pointSizes[[First@index]],
              EventHandler[
               Tooltip[Point@point, points[[All, First@index, 2]]],
               {"MouseMoved" :> (
                  hlPointIndex = Position[#1, point][[1, 1]];
                  pointSizes = initPointSizes;

                  pointSizes[[hlPointIndex]] = 
                   2 initPointSizes[[hlPointIndex]]
                  )}
               ]
              }
             ]]@#1
          } &]@points
     },
    Axes -> True,
    AspectRatio -> 1/GoldenRatio
    ]
   ]
  ]
listPlotWithHighlight[{{1, 2, 3}, {4, 5, 6}}]

enter image description here

glS
  • 7,623
  • 1
  • 21
  • 61