# Plotting Vertical Asymptotes [duplicate]

The following code produces the graph of $f(x)=\frac{1}{x-1}$ correctly.

 Plot[1/(x - 1), {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}},
PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14},
Exclusions -> {x - 1 == 0},
ExclusionsStyle -> Directive[Red, Dashed]]


However, when I try to graph $f(x)=\frac{1}{(x-1)^2}$ with essentially the same code, I lose the vertical asymptote.

Plot[1/(x - 1)^2, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}},
PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14},
Exclusions -> {(x - 1)^2 == 0},
ExclusionsStyle -> Directive[Red, Dashed]]


I have tried all sorts of permutations of writing the exclusions, but nothing seems to work! From what I can tell, whenever I try to graph a rational function that has a factor in the denominator with an even power, the vertical asymptote fails to draw.

Any help would be greatly appreciated!

• It looks like Exclusion "connects" your singularities... So in the first case it connects two points $(1,-\infty)$ and $(1,+\infty)$, in the second case there is just one point $(1,+\infty)$, so no line is visible... Jun 14, 2016 at 19:57

There is a good demonstration of how ExclusionsStyle works here. Check
Plot[Floor[x], {x, 0, 5}, ExclusionsStyle -> {Red, Blue}]

If you use the second form of ExclusionsStyle, you will see the points (boundary of exclusion region) that are being connected by your asymptote:

Plot[1/(x - 1)^3, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 25}},
PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14},
Exclusions -> {x - 1 == 0},
ExclusionsStyle -> {Directive[Red, Dashed],
Directive[PointSize -> 0.05, Green]}]


When you have even powers, those two points degenerate into one point and your line becomes invisible.

A possible workaround would be drawing the line separately with Epilog or ListPlot

Plot[1/(x - 1)^2, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}},
PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14},
Epilog -> {Directive[Red, Dashed], Line[{{1, -1000}, {1, 1000}}]}]


Or

Show[Plot[1/(x - 1)^2, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}},
PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14}],
ListPlot[{{1, -1000}, {1, 1000}}, Joined -> True,
PlotStyle -> Directive[Red, Dashed]]]


Or per Kuba's comment:

Plot[1/(x - 1)^2, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}},
PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14},
GridLines -> {{1}, None}, GridLinesStyle -> Directive[Red, Dashed]]


In the most general case, when your asymptote isn't horizontal or vertical, you will need to plot it as a separate plot. So Exclusions is not the best option:

Plot[{4/(x^2 + 1) + x, x}, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}},
PlotStyle -> {Thick, Directive[Red, Dashed]},  BaseStyle -> {FontSize -> 14}]


• Fantastic! I had no idea that this was how ExclusionStyle worked! I learned two things from this massive headache!
– Jon
Jun 14, 2016 at 20:23
• GridLines are also an option.
– Kuba
Jun 14, 2016 at 20:49
• @Kuba... I like GridLines idea, edited the answer. Jun 14, 2016 at 20:55
• @Kuba on the other hand the GridLines won't work if the asymptote isn't vertical or horizontal... Jun 14, 2016 at 21:35
• @BlacKow, yes, for that you can either plot the oblique asymptote as a separate function, or use InfiniteLine[] in Epilog. Jun 14, 2016 at 23:40

Just for completeness: before InfiniteLine[] came along, I often used Scaled[] in conjunction with Line[] to depict horizontal and vertical asymptotes.

Plot[1/(x - 1)^2, {x, -5, 5}, Axes -> True,
Epilog -> {Directive[Red, Dashed],
Line[{Scaled[{0, 1}, {1, 0}], Scaled[{0, -1}, {1, 0}]}]},
PlotRange -> {{-5, 5}, {-9, 9}}, PlotTheme -> "Detailed"]


Plot[Sec[x]^2, {x, -3 π, 3 π}, Axes -> True,
Epilog -> {Directive[Red, Dashed],
Table[Line[{Scaled[{0, 1}, {(k - 1/2) π, 0}],
Scaled[{0, -1}, {(k - 1/2) π, 0}]}], {k, -2, 3}]},
PlotRange -> {-10, 10}, PlotTheme -> "Detailed"]


Plot[Erf[x], {x, -5, 5}, Axes -> True,
Epilog -> {Directive[Red, Dashed],
Line[{Scaled[{1, 0}, {0, 1}], Scaled[{-1, 0}, {0, 1}]}],
Line[{Scaled[{1, 0}, {0, -1}], Scaled[{-1, 0}, {0, -1}]}]},
PlotRange -> {-2, 2}, PlotTheme -> "Detailed"]


• This technique is still very relevant. I just had to shade the background inbetween $(x_1, x_2)$ and this seems the best way to do it, with a Rectangle in Prolog instead of Line. Jun 28, 2016 at 8:21

Since Mathematica 10, I prefer InfiniteLine for applications like these. It is more flexible than GridLines because it can run in any direction, and it can coexist with, say, GridLines -> Automatic.

Example:

Plot[1/(x - 1)^2, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}},
Epilog -> {Red, Dashed,
InfiniteLine[{1, 0} (* through this point *), {0, 1} (* in a vertical direction *)]},
PlotTheme -> "Detailed", Axes -> True
]