在下面的代码 (MWE) 中,我没有得到函数的虚线tan
,并且图表底部有一个奇怪的勾号。为什么会出现这种情况?如何修复?
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
axis lines=middle,
axis on top,
xlabel=$x$,
ylabel=$y$,
domain=-2*pi:2*pi,
xmin=-7,
xmax=7,
ymin=-5,
ymax=5,
trig format=rad, %<-
xtick={-2*pi,-3*pi/2, -pi, -pi/2,pi/2,pi,3*pi/2,2*pi},
xticklabels={$-2\pi$, $-\frac{3\pi}{2}$, $-\pi$, $-\frac{\pi}{2}$, $\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$},
every axis y label/.style={rotate=0, black, at={(0.5,1.05)},},
every axis x label/.style={rotate=0, black, at={(1.05,0.5)},},,
font=\footnotesize,
]
\addplot[samples=200,smooth]{tan(x)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
问题是tan
域中有极点,这会导致这些影响。因此,我宁愿在循环中单独绘制分支,从而避免极点。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
axis lines=middle,
axis on top,
xlabel=$x$,
ylabel=$y$,
domain=-2*pi:2*pi,
xmin=-7,
xmax=7,
ymin=-5,
ymax=5,
trig format plots=rad, %<-
xtick={-2*pi,-3*pi/2, -pi, -pi/2,pi/2,pi,3*pi/2,2*pi},
xticklabels={$-2\pi$, $-\frac{3\pi}{2}$, $-\pi$, $-\frac{\pi}{2}$, $\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$},
every axis y label/.style={rotate=0, black, at={(0.5,1.05)},},
every axis x label/.style={rotate=0, black, at={(1.05,0.5)},},,
font=\footnotesize,
]
\pgfplotsinvokeforeach{-5,-3,...,3}{
\pgfmathsetmacro{\xmin}{ifthenelse(#1==-5,-2*pi,#1*pi/2+0.01)}
\pgfmathsetmacro{\xmax}{ifthenelse(#1==3,2*pi,#1*pi/2+pi-0.01)}
\addplot[samples=51,smooth,domain=\xmin:\xmax]{tan(x)};
\draw[densely dotted] (#1*pi/2,\pgfkeysvalueof{/pgfplots/ymin})
-- (#1*pi/2,\pgfkeysvalueof{/pgfplots/ymax});
}
\end{axis}
\end{tikzpicture}
\end{document}
还请注意,我使用trig format plots=rad
而不是trig format=rad
。