18

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?

0

3 Answers 3

27

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

3
  • 1
    @Hans-PeterE.Kristiansen Tikz not, but pgfplots yes, you can use TikZ operator r for x to be in radians.
    – osjerick
    Dec 1, 2013 at 3:53
  • @OSjerick: You are right. I am deleting my comment, as the wording has already been changed. Dec 1, 2013 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$ :) Dec 4, 2013 at 13:23
14

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.

enter image description here

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}

enter image description here

1
  • Someone may call it as a bombastic template, yet it is very easy to customize, right? Jul 19, 2014 at 8:32
2

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.

1
  • Nice one, late reply. Is there a way to remove the axes(only just plot)?
    – Shamina
    Jun 10, 2017 at 7:51

Not the answer you're looking for? Browse other questions tagged .