如何繪製 x=-3 線?

如何繪製 x=-3 線?

我已經繪製了一個未在 -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}

f(x)=(x-1)/(x^2+5x+6)

答案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意味著如果您想調整yminymax值,漸近線也會自動調整。

相關內容