Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In the plot below I would like to add two vertical lines at $x = \frac{\pi}{15} \pm \frac{1}{20}$. How can I do that?

f[x_] := (x^2 z)/((x^2 - y^2)^2 + 4 q^2 x^2) /. {y -> π/15, z -> 1, q -> π/600}
Plot[{f[x], f[π/15],f[π/15]/Sqrt[2]}, {x, π/15 - .01, π/15 + .01}]

Plot

share|improve this question
2  
Are you sure you want +-1/20? This is outside your current plot range. – Ajasja Mar 27 '12 at 10:18

4 Answers

up vote 22 down vote accepted

An easy way to add a vertical line is by using Epilog.

Here is an example:

f[x_] := (x^2 z)/((x^2 - y^2)^2 + 4 q^2 x^2) /. {y -> π/15, z -> 1, q -> π/600}
Quiet[maxy = FindMaxValue[f[x], x]*1.1]
lineStyle = {Thick, Red, Dashed};
line1 = Line[{{π/15 + 1/50, 0}, {π/15 + 1/50, maxy}}];
line2 = Line[{{π/15 - 1/50, 0}, {π/15 - 1/50, maxy}}];
Plot[{f[x], f[π/15], f[π/15]/Sqrt[2]}, {x, π/15 - 1/20, π/15 + 1/20},
    PlotStyle -> {Automatic, Directive[lineStyle], Directive[lineStyle]},
    Epilog -> {Directive[lineStyle], line1, line2}]

Vertical lines added

Another, perhaps even easier, option would be using GridLines:

Plot[{f[x]}, {x, π/15 - 1/20, π/15 + 1/20},
    GridLines -> {{π/15 + 1/50 π/15 - 1/50}, {f[π/15], f[π/15]/Sqrt[2]}}, PlotRange -> All]

enter image description here

share|improve this answer
GridLines is more elegant as if the plot is moving along the y axis in a dynamic setup, then the Line object in the Epilog must be updated as well, which requires the calculation of exact {minY, maxY} points. – István Zachar Jul 1 '12 at 8:49

One way is to use GridLines:

f[x_] := (x^2 z)/((x^2 - y^2)^2 + 4 q^2 x^2) /. {y -> π/15, z -> 1, q -> π/600}

Plot[f[x], {x, π/15 - .1, π/15 + .1}, 
 GridLines -> {{Pi/15 - 1/20, Pi/15 + 1/20}, {f[Pi/15], f[Pi/15]/Sqrt[2]}}, 
 PlotRange -> All, Frame -> True, Axes -> False]

Mathematica graphics

share|improve this answer

Can use Show, but Epilog is better.

f[x_] := (x^2 z)/((x^2 - y^2)^2 + 4 q^2 x^2) /. {y -> π/15, z -> 1, q -> π/600}
plot = Plot[{f[x], f[π/15], 
    f[π/15]/Sqrt[2]}, {x, π/15 - .01, π/15 + .01}, PlotRange -> {{0, 0.26}, Automatic}];

Show[plot, 
 Graphics[{Black, Line[{{Pi/15 + 1/20, 2000}, {Pi/15 + 1/20, 9000}}]}],
 Graphics[{Black, Line[{{Pi/15 - 1/20, 2000}, {Pi/15 - 1/20, 9000}}]}]]

enter image description here

share|improve this answer

Another possibility is to use ParametricPlot in tandem with Show:

Show[{
  Plot[{f[x], f[Pi/15], f[Pi/15]/Sqrt[2]}, {x, 0.1, 0.3}, 
   PlotRange -> All, Frame -> True, Axes -> False],

  ParametricPlot[{{Pi/15 + 1/20, u}, {Pi/15 - 1/20, u}}, {u, 0, 9000},
    PlotStyle -> Black]
  }]

ParametricPlot

share|improve this answer

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.