我读了解决方案https://tex.stackexchange.com/a/521359/14423基于正切函数绘制一个函数,但我对渐近线有一个问题:
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=-4*pi:2*pi,xmin=-13,
xmax=7,
ymin=-5,
ymax=15,
trig format plots=rad,
%samples=100,
axis lines=middle,
xlabel=$x$,
xtick={-4*pi,-3*pi,-2*pi,-pi,pi,2*pi},
xticklabels={$-4\pi$,$-3\pi$, $-2\pi$, $-\pi$, $\pi$, $2\pi$},
ylabel=$f(x)$,
legend style={at={(1.45,.9)}}]%legend pos=north east]
\pgfplotsinvokeforeach{-5,-3,...,1}{
\pgfmathsetmacro{\xmin}{ifthenelse(#1==-5,-4*pi,#1*pi-0.01)}
\pgfmathsetmacro{\xmax}{ifthenelse(#1==1,2*pi,#1*pi+2*pi+0.01)}
\addplot[samples=51,smooth,domain=\xmin:\xmax]{-0.5*tan(x/2)-x};
\draw[densely dotted] (#1*pi,\pgfkeysvalueof{/pgfplots/ymin})
-- (#1*pi,\pgfkeysvalueof{/pgfplots/ymax});
}
\end{axis}
\end{tikzpicture}
\end{document}
渐近线的虚线旁边出现了实线,但我不知道这是怎么回事。增加样本也无法解决这个问题。如何解决?
答案1
看Stefan 的回答以获得对该问题的解释。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid,
axis lines=middle,
axis on top,
xlabel=$x$,
ylabel=$y$,
xmin=-7,
xmax=7,
ymin=-5,
ymax=5,
trig format plots=rad,
% https://tex.stackexchange.com/questions/602730 (comment)
restrict y to domain=-5:5, % <-- Added
]
\addplot[samples=501, no markers, smooth, domain=-2*pi:2*pi, red]{tan(x)};
\addplot[samples=501, no markers, smooth, domain=-2*pi:2*pi, blue]{-0.5*tan(x/2)-x};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
像这样:
类似于@Dr. Manuel Kuehner 的回答(他比我回答了几分钟,+1):
\documentclass[border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xmin=-13, xmax=7,
ymin=-5, ymax=15,
restrict y to domain=-20:30, % <---
ticklabel style={font=\footnotesize},
xlabel=$x$,
xtick={-4*pi, -3*pi, -2*pi, -pi, pi, 2*pi},
xticklabels={$-4\pi$, $-3\pi$, $-2\pi$, $-\pi$, $\pi$, $2\pi$},
ylabel=$f(x)$,
legend style={at={(0.5,-0.05)}, anchor=north, font=\scriptsize},
trig format plots=rad,
domain=-4*pi:2*pi, samples=1001, no marks % <---
]%legend pos=north east]
\addplot +[thick]{-0.5*tan(x/2)-x};
\legend{$-0.5\tan(x/2)-x$}
%
\pgfplotsinvokeforeach{-5,-3,...,1}%
{
\draw[densely dotted] (#1*pi,\pgfkeysvalueof{/pgfplots/ymin})
-- (#1*pi,\pgfkeysvalueof{/pgfplots/ymax});
}
\end{axis}
\end{tikzpicture}
\end{document}
编辑:已更正使用的功能。上述解决方案也适用于它。
答案3
按照我帖子中的代码,我注意到我在调整间隔时犯了一个错误。解决方案是将 更改为+0.01
,-0.01
反之亦然,在\xmin
和 中\mxax
。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=-4*pi:2*pi,xmin=-13,
xmax=7,
ymin=-5,
ymax=15,
trig format plots=rad,
%samples=100,
axis lines=middle,
xlabel=$x$,
xtick={-4*pi,-3*pi,-2*pi,-pi,pi,2*pi},
xticklabels={$-4\pi$,$-3\pi$, $-2\pi$, $-\pi$, $\pi$, $2\pi$},
ylabel=$f(x)$,
legend style={at={(1.45,.9)}}]%legend pos=north east]
\pgfplotsinvokeforeach{-5,-3,...,1}{
\pgfmathsetmacro{\xmin}{ifthenelse(#1==-5,-4*pi,#1*pi+0.01)}
\pgfmathsetmacro{\xmax}{ifthenelse(#1==1,2*pi,#1*pi+2*pi-0.01)}
\addplot[samples=250,smooth,domain=\xmin:\xmax]{-0.5*tan(x/2)-x};
\draw[densely dotted] (#1*pi,\pgfkeysvalueof{/pgfplots/ymin})
-- (#1*pi,\pgfkeysvalueof{/pgfplots/ymax});
}
\end{axis}
\end{tikzpicture}
\end{document}