如何在图表中绘制垂直渐近线?

如何在图表中绘制垂直渐近线?

我试图在图表中绘制垂直渐近线,但是做不到。

这是我的代码:

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

答案1

问题是您没有指定兼容版本,因此 pgfplots 以向后模式运行。如果您想保留它,则需要axis cs:在绘制模式中添加坐标。指定一个相当新的版本可能更简单、更好。

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

在此处输入图片描述

如果你需要pgfplots以向后兼容模式运行,你可以使用

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

输出结果相同。不过,我认为最好更新 TeX 安装,因为除了添加功能外,还修复了一些实际错误。

相关内容