pgfplots 繪製有理函數時傳回“維度太大”

pgfplots 繪製有理函數時傳回“維度太大”

我已經使用pgfplots繪圖有一段時間了,最近遇到了需要繪製有理函數圖的情況。其中大多數都效果很好,但其中一個導致我收到“尺寸太大”錯誤。這是我的程式碼:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

  \begin{tikzpicture}
    \begin{axis}[axis lines=middle, ymax=10, ymin=-10, xlabel=$x$, ylabel=$y$]
      \addplot[only marks, scatter, scatter src=explicit, point meta=y] coordinates {
        (-4, 4)
        (-3, 5)
        (-2, 10)
        (-1, -5)
        (0, 0)
        (1, 1)
        (2, 1.429)
      };
      \addplot[smooth, domain=-10:-1.5, variable=\x, samples=301, unbounded coords=jump, variable=\x] {(5 * \x) / (2 * \x + 3)};
      \addplot[smooth, domain=-1.5:10, variable=\x, samples=301, unbounded coords=jump, variable=\x] {(5 * \x) / (2 * \x + 3)};
      \addplot[dashed, domain=-10:10] {5 / 2};
      \draw[dashed] ({axis cs:-1.5,0}|-{rel axis cs:0,0}) -- ({axis cs:-1.5,0}|-{rel axis cs:0,1});
    \end{axis}
  \end{tikzpicture}

\end{document}

-3:-1.5透過將第二個和第三個圖的域分別限制為和可以避免這種情況1.5:3。我不明白如何得到太大的數字,只能想像這是一個小數下溢問題。我該如何解決這個問題?

答案1

unbounded coords只有當您獲得精確的inf座標時,鍵才起作用。但是,如果座標變得太大但inf它沒有嘗試處理該數字,則會出現此錯誤。為了避免此類問題,請使用restrict <x,y> to domain密鑰

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[axis lines=middle, ymax=10, ymin=-10, xlabel=$x$, ylabel=$y$,restrict y to domain=-10:10 ]
      \addplot[only marks, scatter, scatter src=explicit, point meta=y] coordinates {
        (-4, 4)
        (-3, 5)
        (-2, 10)
        (-1, -5)
        (0, 0)
        (1, 1)
        (2, 1.429)
      };
      \addplot[domain=-10:-1.5,samples=301, unbounded coords=discard] {((5*x)/(2*x + 3))};
      \addplot[domain=-1.5:10, samples=301, unbounded coords=discard] {((5*x)/(2*x + 3))};
      \addplot[dashed, domain=-10:10] {5 / 2};
      \draw[dashed] ({axis cs:-1.5,0}|-{rel axis cs:0,0}) -- ({axis cs:-1.5,0}|-{rel axis cs:0,1});
    \end{axis}
  \end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容