26
Table[{Re[5 Exp[I 5/2 t]], Im[5 Exp[I 5/2 t]]}, {t, 0, 6}]
ListPlot[%]

This code plots 6 points in the complex plane. To each point I would like to add a label in the plot that lists the value of t. How can I do that?

Please note: I want to print this plot, so the labels should all be visible.

Verbeia
  • 34,233
  • 9
  • 109
  • 224
sjdh
  • 7,757
  • 5
  • 37
  • 47

3 Answers3

27

All plotting functions are just wrappers for Graphics objects. Show can be used to combine these objects, and that's one way of doing what you want here.

Take the following code as a starting point; you will of course have to tweak the positions of the labels (right now it's just a radial factor that offsets them). Note that I modified the data variable so that it includes $t$; it is now of the form {t, Re, Im}. The ListPlot will plot only the real and imaginary parts (data[[All, {2, 3}]]), while the Text passage also takes into account the value of $t$.

data = Table[{t, Re[5 Exp[I 5/2 t]], Im[5 Exp[I 5/2 t]]}, {t, 0, 6}];
dataPlot = ListPlot[data[[All, {2, 3}]], PlotStyle -> PointSize -> Large];
labels = Text[#[[1]], 1.1 #[[{2, 3}]]] & /@ data;
Show[
    dataPlot,
    Graphics[{Red, labels}],
    PlotRange -> 6 {{-1, 1}, {-1, 1}},
    AspectRatio -> 1
]

enter image description here

You can of course generalize this arbitrarily, e.g. color the labels differently, change font size etc. I kept the above to a minimum to avoid cluttering, the rest is up to you.

David
  • 14,911
  • 6
  • 51
  • 81
  • 3
    Why don't you use an offset for Text? –  Feb 16 '12 at 09:25
  • I felt like a factor fits the radial symmetry nicer here, but you're of course not limited to that. – David Feb 16 '12 at 12:49
  • I think, this does not behave well: data = Table[{t, Re[5 Exp[I 5/2 t]], Im[5 Exp[I 5/2 t]]} + 20, {t, 0, 6}]; dataPlot = ListPlot[data[[All, {2, 3}]], PlotStyle -> PointSize -> Large]; labels = Text[#[[1]], 1.1 #[[{2, 3}]]] & /@ data; Show[dataPlot, Graphics[{Red, labels}], AspectRatio -> 1] –  Feb 16 '12 at 13:34
  • Well you just added 20 to all imaginary parts. If you want a text offset, you'll have to change the 1.1 #[[{2, 3}]] to something like #[[{2, 3}]]+{1,1}. – David Feb 16 '12 at 13:52
  • The point is that with the third argument to Text you do not have to change anything. –  Feb 16 '12 at 14:17
  • Except for the third argument. Which is limited to additive offsets. – David Feb 16 '12 at 14:21
  • I am using this but when there are two points on the same position the labels show on top of each other. I have tried using tally and then position to try to add an offset to each label for a point that is in the same location as another but I can't get Position to work in this instance. – lara Jan 21 '14 at 11:11
24

Just discovered this thread while looking for an answer to the same question and figured I probably couldn't help the OP now, but decided to provide the answer I found for posterity.

You could wrap each point you're plotting in the Labeled function like so:

Table[
  Labeled[
    {Re[5 Exp[I 5/2 t]], Im[5 Exp[I 5/2 t]]},
    t
  ]
  , {t, 0, 6}];
ListPlot[%]

Labeled has options to control placement and whatnot. All in the documentation.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Jacob Jurmain
  • 249
  • 2
  • 2
8

How about:

data = Table[
  Tooltip[{Re[5 Exp[I 5/2 t]], Im[5 Exp[I 5/2 t]]}, t], {t, 0, 6}]
ListPlot[data]

A little bit of a hack...

data = Table[{coord = {Re[5 Exp[I 5/2 t]], Im[5 Exp[I 5/2 t]]}, 
   Text[t, coord, {-2, 0}]}, {t, 0, 6}]
Show[ListPlot[data[[All, 1]]], Graphics[data[[All, 2]]]]

Mathematica graphics

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263