7
$\begingroup$
data = <|
"exp" -> <|15 -> 1419, 16 -> 1454, 17 -> 1526, 18 -> 1560, 19 -> 1579, 20 -> 1433, 21 -> 1572, 22 -> 1625|>, 
"imp" -> <|15 -> 1190, 16 -> 1243, 17 -> 1308, 18 -> 1360, 19 -> 1399, 20 -> 1280, 21 -> 1395, 22 -> 1491|>
|>;

ListLinePlot[data,
 LabelingFunction -> None,
 PlotLabels -> Automatic,
 PlotMarkers -> All,
 PlotHighlighting -> "Ball"]

enter image description here

I have two requests:

  1. The Ball- box should only display if the mouse pointer is on or
    close to one of the PlotMarkers

  2. The PlotLabels ("exp" or "imp") should also appear inside the
    Ball- box

$\endgroup$
2
  • $\begingroup$ Do you only want the Ball to show up for the point closest to the mouse pointer, or the whole set of points when the mouse is close to one of the points? I can kind of get the latter to work with Mouseover, but am having trouble figuring out how to do the first one. $\endgroup$
    – ydd
    Aug 7, 2023 at 17:49
  • $\begingroup$ Only the point closest to the mouse pointer $\endgroup$
    – eldo
    Aug 7, 2023 at 17:52

3 Answers 3

8
$\begingroup$
labelingRules = Join @@ KeyValueMap[Thread[#2 -> #] &] @ 
    Map[KeyValueMap[List]] @ data;

plotHighlighting = {{"IndicatedPoint", <|"Style" -> AbsolutePointSize[12]|>}, 
   {"XYLabel", <|Appearance -> None,  
     LabelingFunction -> 
       (Row[{#[[All, 1]] /. labelingRules, Splice@#}, ","] &)|>}};


ListLinePlot[data, PlotLabels -> Automatic, PlotMarkers -> All, 
 PlotHighlighting -> plotHighlighting]

enter image description here

$\endgroup$
2
  • $\begingroup$ Would it be possible to also show the x- and y-values? For example, "exp, 20, 1280" . $\endgroup$
    – eldo
    Aug 7, 2023 at 22:42
  • $\begingroup$ @eldo, please see the updated version. $\endgroup$
    – kglr
    Aug 7, 2023 at 22:51
3
$\begingroup$

Something like this, maybe?

I'm not partial to the Association, so:

data2 = {#[[1]], #[[2]]} & /@ Normal[#] & /@ {data[[1]], data[[2]]}

{{{15, 1419}, {16, 1454}, {17, 1526}, {18, 1560}, {19, 1579}, {20, 1433}, {21, 1572}, {22, 1625}}, {{15, 1190}, {16, 1243}, {17, 1308}, {18, 1360}, {19, 1399}, {20, 1280}, {21, 1395}, {22, 1491}}}

labels = Keys[data]

{"exp", "imp"}

ListLinePlot[
 Table[Tooltip[#, labels[[n]] <> " " <> ToString[#]] & /@ 
   data2[[n]], {n, 2}], PlotMarkers -> All]

enter image description here

Edited to add a PlotHighlighting option (but I couldn't figure out how to add custom text to the labels)

ListLinePlot[data, PlotLabels -> Automatic, PlotMarkers -> All, 
 PlotHighlighting -> {"IndicatedPoint", "XYLabel"}]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Thank you, this does what I want - but I hoped that I could do it with the PlotHighlighted - option (because it offers more options than Tooltip, marker lines for example). $\endgroup$
    – eldo
    Aug 7, 2023 at 22:15
  • $\begingroup$ @eldo I added something with PlotHighlighting, but I could not figure out how to add "exp" etc to the labels. $\endgroup$
    – MelaGo
    Aug 7, 2023 at 22:44
2
$\begingroup$

An alternative approach: pre-process input data wrapping each data set with Highlighted:

highlightingF[key_] :=    
  {{"IndicatedPoint", <|"Style" -> AbsolutePointSize[12]|>}, 
   {"XYLabel", <|Appearance -> None, 
     LabelingFunction -> (Row[{key, Splice@#}, ","] &)|>}};

toHighlightedData = Association @*
   KeyValueMap[{k, v} |-> k -> Highlighted[v, highlightingF[k]]];

ListLinePlot[toHighlightedData @ data, 
 PlotLabels -> Automatic, PlotMarkers -> All]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ To use other Highlight- options: Change ListLinePlot to ListPlot with Joined->True and IndicatedPoint to f.e. Dropline $\endgroup$
    – eldo
    Aug 8, 2023 at 0:45

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.