Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say I have the following plot Graphics[{Opacity[0.2], Rectangle[{0, 0}, {4, 4}]}]

And I want to label all vertices on the graph to give their coordinates, in this case, Point(0,0) as "P1", Point(0,4) as "P2"...

In general, I want to write a function: plotPolygonWithLabel[polygon_, label_]:=...

where label_ is the array like {"P1", "P2", } here

share|improve this question

There are many methodst to achieve that, You can start with this:

points = Table[2 {Cos@t, Sin@t}, {t, 0, 2 Pi - .2 Pi, .2 Pi}]
labels = Table["P" <> ToString[t], {t, Length@points}]

 f[points_, labels_] :=With[{O = Mean@points}, 
    Text[#1, #2] & @@@ ({labels, ((.2 + Norm[# - O]) ( 
          Normalize[# - O]) + O) & /@ points}\[Transpose])}]]


enter image description here

share|improve this answer

A slightly generalized version of Kuba's answer, by placing labels along the angular bisectors, which I think can handle most simply irregular polygons:

offsetFunc[pts : {Repeated[_List, {3}]}] :=
 Normalize[Most[Cross[{0, 0, 1},
                      Append[Total[Normalize /@ Differences[pts]], 0]

labeledPolygon[points_, labels_, offset_: 1] :=
  Text[Style[#1, Red, Bold],
       offset offsetFunc[#2]] & @@@
   ({labels, Partition[points, 3, 1, {2, 2}]}\[Transpose])


points = Table[RandomReal[{1, 3}] {Cos[t], Sin[t]}, {t, 0, 2 π, π/10}] // Most;

labels = Table["P" <> ToString[t], {t, Length@points}];

Graphics[{EdgeForm[{Lighter@Blue, Thick}], labeledPolygon[points, labels, 2]}]


It may not perform well on non-simple polygon:

counter example

share|improve this answer

A different approach, but needs tweaking for graphical perfection:

plotPolygonWithLabel[polygon_, label_, fontSize_] := 
     Disk[#1, (fontSize + 12)/72],
     Text[Style[#2, Black, fontSize, 
       FontFamily -> "Helvetica Bold", 
       Background -> White], #1]} & , 
   {polygon, label}]}

(* borrowing from Silvia ... *)
points = Table[
   RandomReal[{-3, 3}] {2 Cos[t], 2 Sin[t]}, {t, 0, 2 \[Pi], Pi/5}];

labels = Table["P" <> ToString[t], {t, Length@points}];

Graphics[{plotPolygonWithLabel[points, labels, 14]}]

alternative label style

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.