26
$\begingroup$
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.

$\endgroup$

3 Answers 3

28
$\begingroup$

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.

$\endgroup$
7
  • 3
    $\begingroup$ Why don't you use an offset for Text? $\endgroup$
    – user21
    Commented Feb 16, 2012 at 9:25
  • $\begingroup$ I felt like a factor fits the radial symmetry nicer here, but you're of course not limited to that. $\endgroup$
    – David
    Commented Feb 16, 2012 at 12:49
  • $\begingroup$ 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] $\endgroup$
    – user21
    Commented Feb 16, 2012 at 13:34
  • $\begingroup$ 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}. $\endgroup$
    – David
    Commented Feb 16, 2012 at 13:52
  • $\begingroup$ The point is that with the third argument to Text you do not have to change anything. $\endgroup$
    – user21
    Commented Feb 16, 2012 at 14:17
24
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ This code does not work... $\endgroup$
    – geordie
    Commented Nov 14, 2013 at 23:37
  • 2
    $\begingroup$ @geordie It does for me on 9.0.1. You probably are using an old version... not really the OP's fault or downvote worthy, imo. $\endgroup$
    – rm -rf
    Commented Nov 15, 2013 at 0:14
  • $\begingroup$ Fair enough. Apologies to the OP. $\endgroup$
    – geordie
    Commented Nov 15, 2013 at 5:10
  • $\begingroup$ How would I alter that if my data set were a list of pairs and I didnt have to use Table ? eg data={{1,2},{3,4},{5,6}} and labels={a,b,c}. $\endgroup$
    – lara
    Commented Jan 21, 2014 at 11:08
8
$\begingroup$

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

$\endgroup$
0

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.