Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

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

Image of the plot showing a linear function where the sin function should be

The MWE is here:

\documentclass{standalone}
\usepackage{ pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[samples=500]{sin(x)};
\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?

share|improve this question
add comment

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 10 down vote accepted

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}
\addplot[samples=500,domain=0:2*pi]{sin(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
@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
add comment

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}

enter image description here

share|improve this answer
add comment

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}
            \addplot[samples=500,domain=0:2*pi]{sin(deg(x))};%x in radians, 500 samples into the domain, it's necessary the transformation radians->degrees
        \end{axis}
    \end{tikzpicture}
\end{document}

Result

enter image description here

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.

share|improve this answer
add comment

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