我已经绘制了一个未在 -3 和 -2 处定义的图形函数。但不知何故,我尝试绘制垂直渐近线 x=-3 和 x=-2,然后就卡住了。据我所知,用于\addplot
绘制 f(x),而不是 f(y)。有人能帮帮我吗?
\documentclass{report}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = left, axis x line= center,
axis y line= center,
xlabel = $x$, ylabel = {$f(x)$},
xmajorgrids=true, ymajorgrids=true,
restrict y to domain=-50:50,
ymin=-40, ymax=40,]
\addplot[
samples=500, color=red, thick]
{(x-1)/(x^2+5*x+6)};
\addplot[
samples=100, color=blue, thin, dashed]
% {???}; <= how can I get the x=-3 line?
\end{axis}
\end{tikzpicture}
\end{document}
答案1
添加这些渐近线的一个好方法是使用以下命令\draw
:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
xlabel=\(x\),
ylabel=\(f(x)\),
grid=major,
restrict y to domain=-50:50,
samples=501,
]
\def\ymin{\pgfkeysvalueof{/pgfplots/ymin}}
\def\ymax{\pgfkeysvalueof{/pgfplots/ymax}}
\addplot [red, thick]
{(x-1)/(x^2+5*x+6)};
\draw [red, thin, dashed] (-3, \ymin) -- (-3, \ymax);
\draw [red, thin, dashed] (-2, \ymin) -- (-2, \ymax);
\end{axis}
\end{tikzpicture}
\end{document}
请注意,我擅自简化了一些选项。使用\pgfkeyvaluesof
意味着,如果您要调整ymin
或ymax
值,渐近线也会自动调整。