当我绘制 f(x)=1/x 或类似函数时,pgfplots 会绘制渐近线。我是否必须将函数绘制为具有不同域的两部分,还是我遗漏了什么?
\documentclass[a4paper]{article}
\usepackage{tkz-euclide}
\usepackage{pgfplots}
\usetkzobj{all}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}%
[
grid=major,
x=5mm,
y=5mm,
xtick={-10,-9,...,10},
xmin=-10,
xmax=10,
xlabel={\tiny $x$},
axis x line=middle,
ytick={-10,-9,...,10},
tick label style={font=\tiny},
ymin=-10,
ymax=10,
ylabel={\scriptsize $f(x)$},
axis y line=middle,
no markers,
samples=100,
domain=-10:10,
]
\addplot[thick] (x,{1/x});
\addplot[dashed] (x,{1/(x-2)});
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
答案1
您必须对两个图的restrict y to domain
和 进行一些调整samples
。我不知道为什么,但是如果样本数太低, 的渐近线也可见restrict y to domain
。如果restrict y to domain
太低,您必须大量增加样本数,以便将图形延伸到图的边缘。
但玩弄这两个价值观比用文字解释它们要容易得多,所以尽情享受吧!
\documentclass[a4paper]{article}
\usepackage{tkz-euclide}
\usepackage{pgfplots}
\usetkzobj{all}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}%
[
grid=major,
x=5mm,
y=5mm,
xtick={-10,-9,...,10},
xmin=-10,
xmax=10,
xlabel={\tiny $x$},
axis x line=middle,
ytick={-10,-9,...,10},
tick label style={font=\tiny},
ymin=-10,
ymax=10,
ylabel={\scriptsize $f(x)$},
axis y line=middle,
no markers,
samples=100,
domain=-10:10,
restrict y to domain=-20:20
]
\addplot[thick,samples=400] (x,{1/x});
\addplot[dashed, samples=400] (x,{1/(x-2)});
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}