# How can I plot vertical asymptote in my graph?

I'm trying to plot vertical asymptotes in my graph but I can't.

This is my code:

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}%
[
grid=major,
x=5mm,
y=5mm,
xtick={-15,-14,...,15},
xmin=-15,
xmax=15,
xlabel={\tiny $x$},
axis x line=middle,
ytick={-8,-7,...,8},
tick label style={font=\tiny},
ymin=-8,
ymax=8,
ylabel={\scriptsize $\qquad \qquad \qquad f(x)=\dfrac{x^2-4*x-5}{x^2-9}$},
axis y line=middle,
no markers,
samples=100,
domain=-15:15,
restrict y to domain=-10:10
]
\draw[dashed] ( -3,-8) -- ( -3,+8);
\draw[dashed] ( 3,-8) -- ( 3,+8);
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

• Possibly duplicate: tex.stackexchange.com/a/34201/31034 – ferahfeza Mar 24 at 9:38
• I'm trying to understand why this 2 line " \draw[dashed] ( -3,-8) -- ( -3,+8); \draw[dashed] ( 3,-8) -- ( 3,+8);" aren't working – ryuk Mar 24 at 9:41
• Use axis cs for coordinate definition in draw. \draw[dashed] ( axis cs:-3,-8) -- (axis cs: -3,+8); – ferahfeza Mar 24 at 10:34
• I suggest a tikz-only solution. That's simpler! – Black Mild Mar 24 at 16:36

The issue is that you do not specify the compatibility version, so pgfplots runs in backwards mode. If you want to keep that, you need to add axis cs: to the coordinates in the draw mode. It is arguably simpler and better to specify a reasonably new version.

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{amsmath}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}%
[
grid=major,
x=5mm,
y=5mm,
xtick={-15,-14,...,15},
xmin=-15,
xmax=15,
xlabel={\tiny $x$},
axis x line=middle,
ytick={-8,-7,...,8},
tick label style={font=\tiny},
ymin=-8,
ymax=8,
ylabel={\scriptsize $\qquad \qquad \qquad f(x)=\dfrac{x^2-4*x-5}{x^2-9}$},
axis y line=middle,
no markers,
samples=100,
domain=-15:15,
restrict y to domain=-10:10
]
\draw[dashed] ( -3,-8) -- ( -3,+8);
\draw[dashed] ( 3,-8) -- ( 3,+8);
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}


If you need to run pgfplots in backwards compatibility mode, you could use

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}%
[
grid=major,
x=5mm,
y=5mm,
xtick={-15,-14,...,15},
xmin=-15,
xmax=15,
xlabel={\tiny $x$},
axis x line=middle,
ytick={-8,-7,...,8},
tick label style={font=\tiny},
ymin=-8,
ymax=8,
ylabel={\scriptsize $\qquad \qquad \qquad f(x)=\dfrac{x^2-4*x-5}{x^2-9}$},
axis y line=middle,
no markers,
samples=100,
domain=-15:15,
restrict y to domain=-10:10
]
\draw[dashed] (axis cs: -3,-8) -- (axis cs: -3,+8);
\draw[dashed] (axis cs: 3,-8) -- (axis cs: 3,+8);
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}


which yields the same output. However, I would like to argue it is better to update the TeX installation because, apart from adding features, some real bugs have been fixed.

• when i try to compile your code, there is an error "! Package pgfkeys Error: Choice '1.16' unknown in choice key '/pgfplots/compat/ anchors'. I am going to ignore this key.". Why? maybe "\pgfplotsset{compat=1.16}"? – ryuk Mar 24 at 17:45
• @ryuk 1.16 is the current version. This error message means that you haven't updated your TeX installation in a while. So you could (i) update your TeX installation or (ii) try with a lower version (1.15, 1.14 ...) or (iii) drop \pgfplotsset{compat=1.16} and use \draw[dashed] (axis cs: -3,-8) -- (axis cs: -3,+8); \draw[dashed] (axis cs: 3,-8) -- (axis cs: 3,+8);. – marmot Mar 24 at 17:48
• Thanks! I would linke in the point (11, 4) the write $y=f(x)$. Could you help me? – ryuk Mar 24 at 18:07
• @ryuk \node at (11,4) {$y=f(x)$}; or \node at (axis cs:11,4) {$y=f(x)$}; Notice that there is also the possibility to add a legend, this site is full of examples. – marmot Mar 24 at 18:08
• @ryuk Yes, because you have tick label style={font=\tiny},, so the other labels have font size \tiny, so you may want to try \path (axis cs:0,0) node[below right,font=\tiny] {$0$};. – marmot Mar 25 at 14:41