Weird result plotting sin(x) using tikz/pgf [duplicate]

Trying to plot the sin(x) function, I get this result:

The MWE is here:

\documentclass{standalone}
\usepackage{ pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\end{document}


cos(x) looks like an inverted parabola and tan looks just like sin.

I'm using pdflatex and MiKTeX. Any ideas?

-

marked as duplicate by m0nhawk, Qrrbrbirlbel, Peter Jansson, Claudio Fiandrino, Ludovic C.Dec 1 '13 at 10:55

As x is in radian, you have to use sin(deg(x)) to indicate \deg(x) in degrees in which the domain of trigonometric functions in TikZ is defined.

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\end{document}


-
@Hans-PeterE.Kristiansen Tikz not, but pgfplots yes, you can use TikZ operator r for x to be in radians. –  OSjerick Dec 1 '13 at 3:53
@OSjerick: You are right. I am deleting my comment, as the wording has already been changed. –  Hans-Peter E. Kristiansen Dec 1 '13 at 3:57
Thanks. I did change the domain a bit, but it was still a line, so I figured it was something else. It seems I forgot that when x is near 0, $sin(x) \simeq x$ :) –  facuq Dec 4 '13 at 13:23

A recommended solution with PSTricks just for fun!

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-.5,-1.5)(7,2)
\psaxes[trigLabels,trigLabelBase=2,dx=\dimexpr\psPi cm/2\relax]{->}(0,0)(-.5,-1.5)(6.5,1.5)[$x$,0][$y$,90]
\psplot[algebraic,linecolor=blue]{0}{TwoPi}{sin(x)}
\end{pspicture}
\end{document}


-

The truly important issue here is that pgfplots assign by default 25 values to x in a range of -5 to 5 (independent of degrees or radians), this allows us to obtain by default 25 samples after evaluate the mathematical expression. Change the number of samples only allow us to have more values ​​to graph but only in that interval (-5 to 5). It is necessary to set the domain of the function to be evaluated, considering that the trigonometric functions in pgfplots only works with degrees.

Code

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+[domain=0:360]{sin(x)};%Just setting the domain, 25 samples into the domain
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}
\addplot[samples=500,domain=0:360]{sin(x)};%x in degrees, 500 samples into the domain
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}

Varying the samples number allows you to have more smoothness and a more accurate approximation to the real curve.
From the TikZ manual (pp. 533), x can be in radians by employing the r operator, but this not work with pgfplots in a mathematical expression. The pgfplots manual (pp. 52) recommend to use deg(x) if you want to use radians.