4

I have the following coordinate pairs:

c={{1137, 1008}, {1052, 991}, {1060, 988}, {1137, 1007}, 
  {1189, 1090}, {1125, 1049}, {1189, 1081}, {1248, 1128}, 
  {1251, 1095}, {1129, 1007}, {1016, 964}, {1132, 1059}}

and corresponding labels:

labels = {"a", "a", "b", "b", "c", "c", "d", "d", "e", "e", "f", "f"};

What I want to do:

  1. Plotting coordinates as points with ListPlot

  2. Plotting arrows between coordinate pairs

  3. Plotting corresponding label and coordinate of each point as text

I tried the following:

plot = ListPlot[c, AspectRatio -> Automatic, Frame -> True, 
   PlotMarkers -> {Graphics[{Point[{0, 0}]}]}, 
   FrameLabel -> {{"y", ""}, {"x", ""}}, ImageSize -> Medium];

arrows = Partition[c, 2];

gArrows = Graphics@Arrow[a] /. a -> arrows;

Show[plot, gArrows]

The result is:

enter image description here

I would like to plot close to each point its label and coordinate.

For the left lower point I added this manually:

enter image description here

How can all coordinates and labels be added automatically to the data points?

mrz
  • 11,686
  • 2
  • 25
  • 81

2 Answers2

9

This can take the advantage of new labeling function:

plot = ListPlot[c, AspectRatio -> Automatic, Frame -> True, 
    PlotMarkers -> {Graphics[{Point[{0, 0}]}]}, 
    FrameLabel -> {{"y", ""}, {"x", ""}}, ImageSize -> 500,     
    LabelingFunction -> (Placed[ToString@labels[[#2[[1]]]] <> " " <>
    ToString[#], Automatic] &)]

enter image description here

MinHsuan Peng
  • 2,046
  • 15
  • 14
  • Great ... thank you. What does labels[[#2[[1]]]] ? – mrz Oct 07 '16 at 19:45
  • seems that LabelingFunction didn't work with ListPlot in Mma10.0.2 – Harry Apr 16 '17 at 13:12
  • With Mathematica 11.3 this does not work any more: Always "a" is showed as first part of the labels. Can you reproduce that? – lio Dec 20 '18 at 18:46
  • Hi @lio, since 11.3, the structure of data in LabelingFunction has been extended to include which dataset it belongs to. Change it to labels[[#2[[2]]]] will do. #2[[1]] is always 1 in this case. – MinHsuan Peng Jan 25 '19 at 17:16
8

Callout formatted with Grid

With[{g = Partition[c, 2], lbs = labels[[1 ;; ;; 2]], 
  col = ColorData[97]},
 ListPlot[
  Append[
    MapIndexed[Callout[Style[Mean@#1, col@First@#2],
        Grid[
         Prepend[{"", "x", "y"}]@
          MapThread[
           Prepend, {#1, Table[Subscript[lbs[[First@#2]], i], {i, 2}]}], 
         BaseStyle -> {FontSize -> Small}, 
         Dividers -> {{2 -> Gray}, {2 -> Gray}}], 
        Background -> None] &]@g]@g,
  ColorFunction -> col,
  PlotStyle -> Append[Opacity[0]]@ConstantArray[Automatic, Length@g],
  Prolog -> MapIndexed[{col@First@#2, Arrow@#1} &, g]]
 ]

Mathematica graphics

Also with TableForm

With[{g = Partition[c, 2], lbs = labels[[1 ;; ;; 2]], 
  col = ColorData[97]},
 ListPlot[
  Append[
    MapIndexed[Callout[Style[Mean@#1, col@First@#2],
        TableForm[#1, 
         TableHeadings -> {Table[Subscript[lbs[[First@#2]], i], {i, 2}], {"x", "y"}}, 
         TableSpacing -> {1, .5}, TableAlignments -> Center], 
        Background -> None] &]@g]@g,
  ColorFunction -> col,
  PlotStyle -> Append[Opacity[0]]@ConstantArray[Automatic, Length@g],
  Prolog -> MapIndexed[{col@First@#2, Arrow@#1} &, g]]
 ]

Hope this helps.

Edmund
  • 42,267
  • 3
  • 51
  • 143