8

So I'm creating a scatter plot of a large data array, but need to be able to determine which points are where when I look at the plot, so I'm trying to add labels to them.

names = {hun1, hun2, hun3, hun4, hun5, hun6, hun7, hun8, hun9, hun10, hun11, 
  hun12, dur1, dur2, dur3, dur4, dur5, dur6, dur7, dur8, dur9, dur10, 
  dur11, dur12, rib1, rib2, rib3, rib4, rib5, rib6, rib7, rib8, rib9, 
  rib10, rib11, rib12, tel1, tel2, tel3, tel4, tel5, tel6, tel7, tel8,
   tel9, tel10, tel11, tel12, aut1, aut2, aut3, aut4, aut5, aut6, 
  aut7, aut8, aut9, aut10, aut11, aut12, raa1, raa2, raa3, raa4, raa5,
   raa6, raa7, raa8, raa9, raa10, raa11, raa12, sla1, sla2, sla3, 
  sla4, sla5, sla6, sla7, sla8, sla9, sla10, sla11, sla12, nil1, nil2,
   nil3, nil4, nil5, nil6, nil7, nil8, nil9, nil10, nil11, nil12, 
  shi1, shi2, shi3, shi4, shi5, shi6, shi7, shi8, shi9, shi10, shi11, 
  shi12, yul1, yul2, yul3, yul4, yul5, yul6, yul7, yul8, yul9, yul10, 
  yul11, yul12};

data = {{489.341, 11627.5}, {446.705, 12456.3}, {386.858, 5643.63}, {315.868,
   9861.29}, {244.67, 14731.2}, {182.366, 19736.9}, {132.691, 
  48589.6}, {95.238, 40147.5}, {67.859, 35980.8}, {48.169, 
  49055.2}, {34.127, 54661.4}, {24.155, 39770.1}, {451.693, 
  13675.5}, {400.383, 12456.3}, {334.985, 9630.29}, {264.829, 
  9342.43}, {200.192, 14910.2}, {146.9, 48009.1}, {105.931, 
  42596.8}, {75.665, 16085.8}, {53.779, 17529.6}, {38.126, 
  49582.7}, {26.994, 32746.7}, {19.1, 53688.4}, {291.277, 
  14041.2}, {269.67, 9630.29}, {237.826, 12580.9}, {197.883, 
  25810.9}, {155.694, 29176.7}, {117.295, 54339.3}, {85.893, 
  30893.}, {61.866, 49781.5}, {44.163, 45641.2}, {31.379, 
  30635.7}, {22.242, 36180.8}, {15.747, 27270.5}, {533.805, 
  12456.3}, {451.147, 12003.2}, {359.891, 13097.8}, {273.836, 
  3074.57}, {201.759, 0. + 4665.89 I}, {145.824, 40147.5}, {104.287, 
  45641.2}, {74.168, 46261.5}, {52.597, 37834.9}, {37.246, 
  41738.8}, {26.356, 54486.}, {18.644, 48009.1}, {407.594, 
  5757.88}, {392.768, 8970.49}, {367.401, 9927.63}, {328.613, 
  9462.72}, {277.729, 9581.31}, {221.551, 7411.43}, {168.575, 
  35980.8}, {124.204, 52293.}, {89.77, 54661.4}, {64.2, 
  34401.8}, {45.658, 53147.3}, {32.379, 26558.3}, {374.862, 
  9630.29}, {347.055, 12927.9}, {306.074, 0. + 4022.17 I}, {254.669, 
  5757.88}, {200.372, 11038.6}, {150.955, 18442.5}, {110.541, 
  50335.7}, {79.62, 43431.2}, {56.836, 48167.5}, {40.383, 
  16931.3}, {28.625, 54015.}, {20.265, 53688.4}, {478.106, 
  13561.3}, {414.052, 13723.2}, {338.072, 11910.4}, {261.87, 
  14459.8}, {195.186, 49420.}, {142.019, 51231.}, {101.933, 
  51265.1}, {72.63, 41053.1}, {51.556, 49420.}, {36.526, 
  51798.8}, {25.853, 18222.4}, {18.29, 28251.1}, {324.629, 
  9023.59}, {300.548, 13479.6}, {265.058, 8166.31}, {220.542, 
  10792.2}, {173.521, 55471.2}, {130.726, 41738.8}, {95.728, 
  20899.6}, {68.95, 32465.}, {49.22, 48648.3}, {34.972, 
  49781.5}, {24.789, 17529.6}, {17.55, 33478.4}, {551.52, 
  9148.6}, {528.04, 0. + 4678.09 I}, {488.87, 6902.95}, {431.143, 
  7637.19}, {358.733, 12668.6}, {282.249, 9023.59}, {212.638, 
  14761.6}, {155.71, 41053.1}, {112.154, 8821.32}, {80.061, 
  42596.8}, {56.885, 26558.3}, {40.321, 34870.4}, {431.663, 
  12456.3}, {382.629, 0. + 4022.17 I}, {320.13, 7318.95}, {253.085, 
  12003.2}, {191.314, 32227.8}, {140.386, 35329.3}, {101.234, 
  34870.4}, {72.31, 55009.9}, {51.394, 28834.2}, {36.435, 
  18222.4}, {25.797, 55009.9}, {18.253, 48433.1}}

I use ListPlot[data] to generate a scatterplot of the datapoints, but I want to attach their names to the graph. I've tried several different suggestions I've found for similar things on here but get an error:

Coordinate 1.1 should be a pair of numbers or a Scaled or Offset form.

Brett Champion
  • 20,779
  • 2
  • 64
  • 121
  • Do you intend to export the figure? If it's only for your own you can more easily use ToolTip. If not it's going to be a mess. – Öskå Sep 28 '14 at 01:43
  • I tried that as a quick fix, but it won't label each point in my data plot with the corresponding name. it just names each point the entire name array – cosmosoudner Sep 28 '14 at 01:54

3 Answers3

12

Just for something different (courtesy of idea of Kuba...cannot find reference):

ds = Thread[{(ToString /@ names), Re@data}];
f = DynamicModule[{col = Black}, 
    DynamicWrapper[Dynamic@Style[#, col, Bold], 
     If[CurrentValue["MouseOver"], col = Red; pos = #2, col = Black; 
      pos = {}]]] &;
With[{d1 = ds}, 
 Column[{ListPlot[{#2} & @@@ d1, 
    BaseStyle -> {PointSize@.02, 15, Bold}, 
    Epilog -> {Thick, PointSize@.05, Opacity@.5, Dynamic@Point[pos]}, 
    ImageSize -> 400], Grid@Partition[f @@@ d1, 10]}, Frame -> All]]

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
  • Nice! I have been trying without success to do something similar; you just beat me by several hours:) ++1 – kglr Sep 28 '14 at 09:02
  • @kguler credit should go to Kuba...adapted code ... I like DynamicWrapper – ubpdqn Sep 28 '14 at 09:09
6

If tooltip works with you this may help:

ListPlot[MapThread[Tooltip, {data, names}]]

If you want to label all points then try this:

ListPlot[MapThread[Labeled[#1, Style[#2, Bold, 12]] &, {Re[data], names}]]
Brett Champion
  • 20,779
  • 2
  • 64
  • 121
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
  • (+1) Interesting idea with Labeled: it seems that Mathematica tries to position the labels automatically in such a way that they do not go beyond the plot range. AFAIK this behavior is undocumented. – Alexey Popkov Sep 28 '14 at 07:31
6

Using Tooltip[] and colors to identify sets:

names = ToString /@ names;
data = Re@data; 
ts = TooltipStyle -> {10, Background -> LightRed, CellFrame -> 3};
abbrevs = Union[StringTake[#, 3] & /@ names];
col[x_String] := ColorData[35][Position[abbrevs, StringTake[x, 3]][[1, 1]]]
legend = Framed@ SwatchLegend[ Sequence @@ Transpose[{col@#, #} & /@ abbrevs]]; 
Framed@Row[{Graphics[{col@#, Tooltip[Point@#2, #, ts]} & @@@ Transpose[{names, data}],
                     AspectRatio -> 1, Axes -> True, AxesOrigin -> {0, 0}, ImageSize -> 300], 
           legend}]

Mathematica graphics

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453