太搞笑了!
我正在尝试绘制曲线y=tan(x)。
- 当我在域中绘制曲线时(-π/2,π/2)一切正常(参见第一个代码和图像)。
- 当我在其他域(例如)绘制曲线时,
(π/2,3π/2)
出现了一些问题(参见第二个代码和图像)。
发生了什么事?我该如何解决?
域=(-π/2,π/2)
\documentclass{article}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[
axis x line=center,
axis y line=center,
xlabel={$x$},
ylabel={$y$},
xlabel style={below right},
ylabel style={above left},
xmin=-8.5,
xmax=8.5,
ymin=-8,
ymax=8,
xtick={-3*(pi/2),-pi,-pi/2,0,pi/2,pi,3*pi/2},
xticklabels={$-\frac{3\pi}{2}$,$-\pi$,$-\frac{\pi}{2}$,$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$}]
\addplot [domain=-pi/2:pi/2,ultra thick,smooth] {tan(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}
域=(-π/2,π/2)
\documentclass{article}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[
axis x line=center,
axis y line=center,
xlabel={$x$},
ylabel={$y$},
xlabel style={below right},
ylabel style={above left},
xmin=-8.5,
xmax=8.5,
ymin=-8,
ymax=8,
xtick={-3*(pi/2),-pi,-pi/2,0,pi/2,pi,3*pi/2},
xticklabels={$-\frac{3\pi}{2}$,$-\pi$,$-\frac{\pi}{2}$,$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$}]
\addplot [domain=pi/2:3*pi/2,ultra thick,smooth] {tan(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}
探索我们的家园!
答案1
获得所需结果的最简单方法是添加restrict y to domain
键并设置适当的值。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=center,
axis y line=center,
xlabel={$x$},
ylabel={$y$},
xlabel style={below right},
ylabel style={above left},
xmin=-8.5,
xmax=8.5,
ymin=-8,
ymax=8,
smooth,
restrict y to domain=-10:10, % <-- added
]
\addplot [domain=pi/2:3*pi/2,thick] {tan(deg(x))};
\addplot [domain=-pi/2:pi/2,ultra thick] {tan(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
你可以试试
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[axis line style = very thick,
ymin=-4,ymax=4,
xmin=-2,xmax=5.5,
%clip=false,
xtick=\empty,
ytick=\empty,
extra x ticks={-1.5708, 1.5708, 4.71239},
extra x tick labels={$-\frac{\pi}{2}$, $\frac{\pi}{2}$,$\frac{3\pi}{2}$},
every extra x tick/.style={
xticklabel style={anchor=north west},
grid=major,
major grid style={thick,dashed,black}
},
axis lines = center,
xlabel=$x$,ylabel=$y$,
domain=-.5*pi:.5*pi,
samples=200,
]
\addplot [black,ultra thick] {tan(deg(x))};
\addplot [domain=pi/2:3*pi/2,ultra thick] {tan(deg(x))};
\node at (axis cs:0.2, -0.28) {$O$} ;
\node at (axis cs:3.14, -0.28) {$\pi$} ;
\end{axis}
\end{tikzpicture}
\end{document}