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

I often use Epilog to label lines within graphs, and I like to coordinate the color of the labels with the color of the lines they're labeling. This is no problem. However, I'm currently working on a project in which I need vertical connecting lines at selected places, and these should color match as well. For the life of me, I can't figure out how to do this.

Plot[{Sin[x], Sin[x + 0.5]}, {x, 0, 2*Pi}, PlotStyle -> {Blue, Red}, 
 Epilog -> {
   Line[{{2, 0}, {2, Sin[2]}}],
   Line[{{3, 0}, {3, Sin[3.5]}}]}]

enter image description here

Any help would be appreciated.

share|improve this question
up vote 7 down vote accepted
   Plot[{Sin[x], Sin[x + 0.5]}, {x, 0, 2*Pi}, PlotStyle -> {Blue, Red}, 
   Epilog -> {Blue, Line[{{2, 0}, {2, Sin[2]}}], Red, 
   Line[{{3, 0}, {3, Sin[3.5]}}]}]
share|improve this answer
That works. Stackexchange won't let me accept the answer yet but I'll give you the checkmark in the morning. – Michael Stern Feb 26 '13 at 2:57

kguler's method gets the job done but in anything but the simplest uses it can become a headache.

In cases like this I prefer to use either a custom plot function or a function/rule that accepts plot output and applies my changes automatically. For example:

addEpilog[g_Graphics, epi_List] :=
  With[{styles = Cases[g[[1]], {dir__, __Line} :> Directive[dir], -5]},
    Show[g, Epilog -> {styles, epi} ~Flatten~ {2}]


p = Plot[{Sin[x], Sin[x + 0.5]}, {x, 0, 2*Pi}, PlotStyle -> {Blue, Red}];

  { Line[{{2, 0}, {2, Sin[2]}}], Line[{{3, 0}, {3, Sin[3.5]}}] }

Mathematica graphics

To add multiple primitives in a given style simply group them in a List:

  Line[{{#, 0}, {#, Sin[#]}}] &      /@ {2, 4, 6},
  Line[{{#, 0}, {#, Sin[# + .5]}}] & /@ {1, 3, 5}

Mathematica graphics

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.