# 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!

## Setting Description

The following figure describes the settings used in the code. You just need to change some settings to meet your requirement. As the code is self-explanatory, I think I don't need to speak too much here. Let my code speaks.

## An example

\documentclass[pstricks,border=0pt,12pt,dvipsnames]{standalone}
\usepackage{amsmath}
\usepackage{pst-plot}
\usepackage{pst-math}
\usepackage[nomessages]{fp}

\FPeval\XMin{0-2*pi}
\FPeval\XMax{2*pi}
\FPeval\YMin{0-pi}
\FPeval\YMax{5*pi/3}

\FPeval\XOL{0-1/3} % of DeltaX
\FPeval\XOR{1/3} % of DeltaX
\FPeval\YOB{0-1/3} % of DeltaY
\FPeval\YOT{1/3} % of DeltaY

\FPset\TrigLabelBase{3}
\FPeval\DeltaX{pi/TrigLabelBase}
\FPeval\DeltaY{pi/TrigLabelBase}

\FPeval\AxisL{XMin+DeltaX*XOL}
\FPeval\AxisR{XMax+DeltaX*XOR}
\FPeval\AxisB{YMin+DeltaY*YOB}
\FPeval\AxisT{YMax+DeltaY*YOT}

\newlength\Width\Width=12cm
\newlength\Height\Height=8cm

\newlength\llx\llx=-5pt
\newlength\urx\urx=15pt
\newlength\lly\lly=-5pt
\newlength\ury\ury=15pt

\psset
{
llx=\llx,
lly=\lly,
urx=\urx,
ury=\ury,
xtrigLabels=true,
ytrigLabels=true,
trigLabelBase=\TrigLabelBase,
labelFontSize=\scriptstyle,
xAxisLabel=$x$,
yAxisLabel=$y$,
algebraic,
plotpoints=500,
}

\def\f{Pi*sin(x)}
\def\fp{Derive(1,\f)}

\def\g{2*ASIN(x/(2*Pi))}

\begin{document}
\pslegend[lt]{%
\color{NavyBlue}\rule{12pt}{1pt} & \color{NavyBlue} $y=\pi\sin x$\\
\color{Red}\rule{12pt}{1pt} & \color{Red} $y=\pi\cos x$\\
\color{ForestGreen}\rule{12pt}{1pt} & \color{ForestGreen} $y=2\sin^{-1}\left(\tfrac{x}{2\pi}\right)$
}
\begin{psgraph}
[
dx=\DeltaX,
dy=\DeltaY,
linecolor=gray,
tickcolor=gray,
ticksize=-3pt 3pt,
axespos=top,
]{<->}(0,0)(\AxisL,\AxisB)(\AxisR,\AxisT){\dimexpr\Width-\urx+\llx}{!}%{\dimexpr\Height-\ury+\lly}
\psaxes
[
dx=\DeltaX,
dy=\DeltaY,
labels=none,
subticks=5,
tickwidth=.4pt,
subtickwidth=.2pt,
tickcolor=Red!30,
subtickcolor=ForestGreen!30,
xticksize=\YMin\space \YMax,
yticksize=\XMin\space \XMax,
subticksize=1,
](0,0)(\XMin,\YMin)(\XMax,\YMax)
\psplot[linecolor=NavyBlue]{\XMin}{\XMax}{\f}
\psplot[linecolor=Red]{\XMin}{\XMax}{\fp}
\psplot[linecolor=ForestGreen]{\XMin}{\XMax}{\g}
\end{psgraph}
\end{document}


-
Someone may call it as a bombastic template, yet it is very easy to customize, right? –  In PSTricks we trust Jul 19 at 8:32

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.