2

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
]
\addplot[thick,samples=800] (x,{(x^2-4*x-5)/(x^2-9});
\addplot[thick,dashed, samples=800] (x,1);
\draw[dashed] ( -3,-8) -- ( -3,+8);
\draw[dashed] ( 3,-8) -- ( 3,+8);
 \end{axis} 
\end{tikzpicture}
\end{center}
\end{document}
ryuk
  • 2,257
  • Possibly duplicate: https://tex.stackexchange.com/a/34201/31034 –  Mar 24 '19 at 09: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 '19 at 09:41
  • Use axis cs for coordinate definition in draw. \draw[dashed] ( axis cs:-3,-8) -- (axis cs: -3,+8); –  Mar 24 '19 at 10:34
  • I suggest a tikz-only solution. That's simpler! – Black Mild Mar 24 '19 at 16:36

1 Answers1

4

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
]
\addplot[thick,samples=800] (x,{(x^2-4*x-5)/(x^2-9});
\addplot[thick,dashed, samples=800] (x,1);
\draw[dashed] ( -3,-8) -- ( -3,+8);
\draw[dashed] ( 3,-8) -- ( 3,+8);
\end{axis} 
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

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
]
\addplot[thick,samples=800] (x,{(x^2-4*x-5)/(x^2-9});
\addplot[thick,dashed, samples=800] (x,1);
\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 '19 at 17:45
  • 2
    @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);. –  Mar 24 '19 at 17:48
  • Thanks! I would linke in the point (11, 4) the write $y=f(x)$. Could you help me? – ryuk Mar 24 '19 at 18:07
  • 1
    @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. –  Mar 24 '19 at 18:08
  • How can I add "0" close to the origin of the cartesian plane? I can only add manually with "\draw[fill] (axis cs:0,0) circle [radius=0pt] node[below right] {\scriptsize $0$};", but "0" looks bigger than other numbers. – ryuk Mar 25 '19 at 09:34
  • 1
    @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$};. –  Mar 25 '19 at 14:41