7

I want to plot a graph of tangent function, but it appears as a straight line instead. What is the error? Why does it not compile?

\documentclass{minimal}

\usepackage{tikz,pgfplots}
\usepackage{xcolor}

\begin{document}
\begin{center}

\begin{tikzpicture}[domain=-4:7] [scale=0.8]
    \draw[ultra thick, ->] (-4,0) -- (7.5,0) node[right] {$\textbf{X}$};
    \draw[ultra thick,->] (0,-2) -- (0,4.2) node[above] {$\textbf{Y}$};
    \draw[dashed, thick,red] (1.57,0) -- (1.57,1);
    \draw[dashed, thick,red] (4.71,0) -- (4.71,1);
    \draw[dashed, thick,red] (-1.57,0) -- (-1.57,1);
    \draw[ultra thick,color=blue]   plot [samples =100](\x,tan(\x r)) node[right] {$y = \tan~x$};
\end{tikzpicture}

\end{center}
\end{document}
  • 2
    I think you need to include the tan bit in braces, like {tan(\x r)}. To quote section 22.5 of the tikz/pgf manual: Note that you will often have to put the x- or y-coordinate inside braces, namely whenever you use an expression involving a parenthesis. – David R Sep 30 '15 at 14:46
  • David Robertson, Sorry, I did, but it doesn't work – benedito Sep 30 '15 at 14:50
  • 2
    The problem is the domain and the singularity of the tan - function at \pi/2 – user31729 Sep 30 '15 at 14:51
  • I know this is 4 years later, but for people who might be reading along: It took me a while to realise that the trig functions take a degree input rather than a radian input. So rather than specifying your domain as -pi:pi, go for -80:80 (don't go for a full 90 because tan(90) blows up to infinity). And then I also divided my \x by 90 in the plot: plot ({\t/90},{tan(\t)}) – so that 1 unit is equal to π/2 radians. – Matt Feb 15 at 19:11
11

Piece of cake with pgfplots:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis lines=middle,
    xmin=-4.3,xmax=7.8,ymin=-4.5,ymax=4.5,
    xlabel={$x$},
    ylabel={$y$}
    ]
  \addplot[blue,samples=200] {tan(deg(x))}node[right]{$y=\tan x$};
\end{axis}

\end{tikzpicture}

\end{document}

enter image description here

|improve this answer|||||
5

The problem seems to be the fact that tan takes on arbitrarily large values near its asymptotes. To get around this, I broke the domain into four pieces, and drew each one using a loop.

Attempted solution

\documentclass{minimal}
\usepackage{tikz}
\usepackage{xcolor}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.8]
\draw[ultra thick, ->] (-4,0) -- (7.5,0) node[right] {$\textbf{X}$};
\draw[ultra thick,->] (0,-2) -- (0,4.2) node[above] {$\textbf{Y}$};

\foreach \i in {-1, 0, 1, 2}{
    \pgfmathsetmacro{\start}{\i*pi-1.3}
    \pgfmathsetmacro{\left}  {(\i-0.5)*pi}
    \pgfmathsetmacro{\end}  {\i*pi+1.3}
    \draw[dashed, thick,red] (\left,-3) -- (\left,3); 
    \draw[ultra thick, color=blue] plot [domain=\start:\end, samples=25] (\x, {tan(\x r)} );
}
\node[blue, right] at (8, 2) {$y = \tan(x)$};
\end{tikzpicture}
\end{center}
\end{document}
|improve this answer|||||
3

This part [domain=-4:7] [scale=0.8] is wrong since it cannot handle two option blocks unlike the path parsing.

You need parentheses around tangent because otherwise it thinks the expression is finished on the first parentheses as David Robertson quoted.

Also the line you are seeing is the top of the diverging plot because the domain is too large and that's TikZ trying to fit the graph on a page hence pushing the rest out of the page.

Instead use pgfplots

|improve this answer|||||
3

You should include the {tan(\x r)} between two braces and specify the domain -pi/2 < x < pi/2 taking care of infinities near the |pi/2|. This is not the best way to draw a function like this, but I just corrected the error you requested. The way that @HarishKumar solved it is the way I would use.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{center}
\begin{tikzpicture}[domain=-4:7] [scale=0.8]
\draw[ultra thick, ->] (-4,0) -- (7.5,0) node[right] {$\textbf{X}$};
\draw[ultra thick,->] (0,-2) -- (0,4.2) node[above] {$\textbf{Y}$};
\draw[dashed, thick,red] (1.57,0) -- (1.57,1);
\draw[dashed, thick,red] (4.71,0) -- (4.71,1);
\draw[dashed, thick,red] (-1.57,0) -- (-1.57,1);
\draw[ultra thick,color=blue] plot[domain=-.4*pi:.4*pi] (\x,{tan(\x r)}) node[right] {$y=\tan x$};
\end{tikzpicture}
\end{center}

\end{document}

enter image description here

|improve this answer|||||
0

I just changed @AboAmmar response a little bit. Hope it helps

\begin{tikzpicture}[domain=-4:7] [scale=0.8]
% x and y axes
\draw[thick, ->] (-6,0)     -- (6,0)   node[right] {$x$};
\draw[thick, ->] (0,-4.2)   -- (0,4.2) node[above] {$y$};
% vertical asymptotes
\draw[dashed, thick,red] (pi/2,-4.2)    --  (pi/2,4.2);
\draw[dashed, thick,red] (3*pi/2,-4.2)  --  (3*pi/2,4.2);
\draw[dashed, thick,red] (-pi/2,-4.2)   --  (-pi/2, 4.2);
\draw[dashed, thick,red] (-3*pi/2,-4.2) --  (-3*pi/2, 4.2);
% tangent between vertical asymptotes
\draw[ultra thick,color=red, <->] plot [domain=-0.425*pi:0.425*pi] (\x,{tan(\x r)});
\draw[ultra thick,color=red, <->] plot [domain= 0.573*pi:1.425*pi] (\x,{tan(\x r)});
\draw[ultra thick,color=red, <->] plot [domain= -1.425*pi:-0.573*pi] (\x,{tan(\x r)});
% x tick labels
\node[below right] at (-3*pi/2, 0)  {$-\frac{3\pi}{2}$};
\node[below right] at (-pi, 0)      {$-\pi$};
\node[below right] at (-pi/2, 0)    {$-\frac{\pi}{2}$};
\node[below right] at (0, 0)        {$0$};
\node[below right] at (pi/2, 0)     {$\frac{\pi}{2}$};
\node[below right] at (pi, 0)       {$\pi$};
\node[below right] at (3*pi/2, 0)   {$\frac{3\pi}{2}$};
\end{tikzpicture}

here what you should get:

enter image description here

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.