4
$\begingroup$

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?

$\endgroup$
1
  • 1
    $\begingroup$ Does this help? (Not the answer, 'cause it's about styling, but the approach in the question itself.) $\endgroup$
    – corey979
    Oct 7, 2016 at 16:46

2 Answers 2

9
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ Great ... thank you. What does labels[[#2[[1]]]] ? $\endgroup$
    – mrz
    Oct 7, 2016 at 19:45
  • $\begingroup$ seems that LabelingFunction didn't work with ListPlot in Mma10.0.2 $\endgroup$
    – Harry
    Apr 16, 2017 at 13:12
  • $\begingroup$ With Mathematica 11.3 this does not work any more: Always "a" is showed as first part of the labels. Can you reproduce that? $\endgroup$
    – lio
    Dec 20, 2018 at 18:46
  • $\begingroup$ 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. $\endgroup$ Jan 25, 2019 at 17:16
8
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ Perfect ... this is publication ready ... thanks a lot $\endgroup$
    – mrz
    Oct 7, 2016 at 20:37
  • $\begingroup$ How is it possible NOT to show a point at the center of each arrow? $\endgroup$
    – mrz
    Oct 8, 2016 at 9:10
  • 1
    $\begingroup$ @mrz Add PlotStyle -> Append[Opacity[0]]@ConstantArray[Automatic, Length@g] to the ListPlot options. $\endgroup$
    – Edmund
    Oct 8, 2016 at 9:17

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.