# How to add a vertical line to a plot

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


• Are you sure you want +-1/20? This is outside your current plot range. Mar 27, 2012 at 10:18
• A related question. Jun 4, 2013 at 6:48

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


# Caveat

While adding lines as Epilog (or Prolog) objects works most cases, the method can easily fail when automated, for example by automatically finding the minimum and maximum of the dataset. See the following examples where the red vertical line is missing at $$x=5$$:

data1 = Table[0, {10}];
data2 = {1., 1., 1.1*^18, 1., 6., 1.2, 1., 1., 1., 148341.};

Row@{
ListPlot[data1, Epilog -> {Red, Line@{{5, Min@data1}, {5, Max@data1}}}],
ListPlot[data2, Epilog -> {Red, Line@{{5, Min@data2}, {5, Max@data2}}}]
}


In the left case, Min and Max of data turned out to be the same, thus the vertical line has no height. For the second case, Mathematica fails to draw the line due to automatically selected PlotRange (selecting PlotRange -> All helps). Furthermore, if the plot is part of a dynamical setup, and the vertical plot range is manipulated, the line endpoints must be updated accordingly, requiring extra attention.

# Solution

Though all of these cases can be handled of course, a more convenient and easier option would be to use 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]


And for the extreme datasets:

Row@{
ListPlot[data1, GridLines -> {{{5, Red}}, None}],
ListPlot[data2, GridLines -> {{{5, Red}}, None}]
}


• @Istvan thanks for the edit. Jun 4, 2013 at 12:56
• Welcome Ajasja. A certain data2 & Epilog combo pissed me off recently triggering this edit. Sadly, I can't give a sound explanation on why Mathematica fails to draw the Line in that case. Perhaps someone else has an insight on this. Jun 4, 2013 at 16:03
• In the Epilog version, I'd personally use Scaled[] instead of futzing around with bounds. Witness for instance ListPlot[{1., 1., 1.1*^18, 1., 6., 1.2, 1., 1., 1., 148341.}, Epilog -> {Blue, Line[{Scaled[{0, -1}, {5, 0}], Scaled[{0, 1}, {5, 0}]}]}]. Jun 5, 2013 at 11:26
• "Who knew..." - I did. ;) Jun 5, 2013 at 12:44
• You can also use InfiniteLine Mar 16, 2018 at 0:43

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]


I assume you mean $$x = \frac{\pi}{15} \pm \frac{1}{200}$$. Then you can use Prolog or Epilog with InfiniteLine, like this:

Plot[
f[x],
{x, π/15 - .01, π/15 + .01},
Epilog -> {
InfiniteLine[{π/15 + 1/200, 0}, {0, 1}],
InfiniteLine[{π/15 - 1/200, 0}, {0, 1}]
}
]


This does not require you to know the plot range, nor any of the function values. In addition, you are still free to use GridLines for actual grid lines.

In case you don't want to cross the $$x$$-axis, you can use HalfLine instead of InfiniteLine, and fix the position of the axis with the AxesOrigin option:

Plot[
f[x],
{x, π/15 - .01, π/15 + .01},
AxesOrigin -> {Automatic, 0},
Epilog -> {
HalfLine[{π/15 + 1/200, 0}, {0, 1}],
HalfLine[{π/15 - 1/200, 0}, {0, 1}]
}
]


• I use InfiniteLine all the time for this sort of thing. Works if you log plot and as you say you don't have to worry about scaling etc. To minimise typing I would write InfiniteLine[{#, 0}, {0, 1}] & /@ {\[Pi]/15 + 1/20, \[Pi]/15 - 1/20} Highly recommended approach.
– Hugh
Oct 20, 2021 at 9:13

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


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


Another possibility is to use Ticks:

Plot[{f[x], f[π/15], f[π/15]/Sqrt[2]}, {x, π/15 - .06, π/15 + .06},
Ticks -> {{{π/15 + 1/20, π/15 + 1/20, {0.595, 0}, Directive[Red, Dashed]},
{π/15 - 1/20, π/15 - 1/20, {0.595, 0}, Directive[Blue, Dashed]}},
All}, PlotRange -> {{0.12, 0.3}, All}]

• Clever manipulation of the Ticks specification, +1. Jun 4, 2013 at 12:47

Next possibility is to use ListPlot:

gp1 = Plot[{({f[x], f[π/15], f[π/15]/Sqrt[2]},{x, π/15 - .05, π/15 + .05}];

ymax = Max[Last /@ Level[Cases[%, _Line, Infinity], {-2}]];

gp2 = ListPlot[{{{π/15 - 1/20, 0}, {π/15 - 1/20,ymax}}, {{π/15 + 1/20, 0}, {π/15 + 1/20, ymax}}},
Joined -> True, PlotRange -> {{0.15, 0.26}, All}];
Show[{gp1, gp2}]

• You might want to note how I formatted your previous answer, and apply that formatting to this answer... Jun 4, 2013 at 7:11
• You may also find it valuable to register your account. Jun 4, 2013 at 12:47