Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
add comment

2 Answers

up vote 6 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
add comment

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}]
  ]

Now:

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

addEpilog[p,
  { 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:

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

Mathematica graphics

share|improve this answer
add comment

Your Answer

 
discard

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.