2
$\begingroup$

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

$\endgroup$
3
  • 2
    $\begingroup$ Have you taken a look at Tooltip in the documentation? $\endgroup$
    – MarcoB
    Jul 27, 2016 at 16:35
  • $\begingroup$ 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 $\endgroup$
    – no-one
    Jul 27, 2016 at 16:44
  • 1
    $\begingroup$ I think you really want BubbleChart! $\endgroup$
    – Jens
    Aug 3, 2016 at 6:44

2 Answers 2

6
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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 $\endgroup$
    – no-one
    Aug 2, 2016 at 14:45
5
$\begingroup$

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

$\endgroup$

Your Answer

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

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