![如何在图表中绘制垂直渐近线?](https://linux22.com/image/392775/%E5%A6%82%E4%BD%95%E5%9C%A8%E5%9B%BE%E8%A1%A8%E4%B8%AD%E7%BB%98%E5%88%B6%E5%9E%82%E7%9B%B4%E6%B8%90%E8%BF%91%E7%BA%BF%EF%BC%9F.png)
我试图在图表中绘制垂直渐近线,但是做不到。
这是我的代码:
\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 安装,因为除了添加功能外,还修复了一些实际错误。