我已经使用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}