1

I am plotting a sinus function using a pgfplot and would like that the tick of the x axis was labeled using multiplers of pi (pi/4, pi/2, 2*pi...), for they are the "important" x values to visualize when studying the sinus function.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}

\begin{tikzpicture}

\begin{axis}[domain=0:2*pi, ymax=1.2, samples=100, grid=major,
  xlabel=$\alpha$, ylabel=$sin(\alpha)$, 
  xtick = {0,1,...,6}, ytick = {-1,-0.5,...,1}] %I assume this the part that I need to change?
    \addplot [blue](\x, {sin(\x r)});
\end{axis}

\end{tikzpicture}
\end{document}

I've tried giving regular values of pi in the braces near the % in the code above, but neither using "pi" nor "\pi" has proven useful. Am I missing something? The plot looks like this using the code above:

This is the plot so far. Sorry for the size of the image.

Álvaro
  • 415

2 Answers2

3

an (simple) alternative:

  • drawing sin(x) (in degrees)
  • label tick with 0, \frac{\pi}{4}, ....

    \documentclass[margin=3mm]{standalone}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.14}
    
    \begin{document}
        \begin{tikzpicture}
    \begin{axis}[
           grid,
           ymax = 1.2,
         xlabel = $\alpha$, ylabel=$\sin(\alpha)$,
          xtick = {0,45,...,360},
    xticklabels = {\mathstrut 0,
                    $\frac{ \pi}{4}$,$\frac{ \pi}{2}$,$\frac{3\pi}{4}$,$\mathstrut \pi$,
                    $\frac{5\pi}{4}$,$\frac{3\pi}{2}$,$\frac{7\pi}{4}$,$\mathstrut2\pi$},
         domain = 0:360,
        samples = 73,
        no marks
                    ]
        \addplot +[very thick] {sin(x)};
    \end{axis}
        \end{tikzpicture}
    \end{document}
    

enter image description here

Zarko
  • 296,517
2

Welcome to TeX.SE! Here is a possibility. Notice that there exists a number format frac, but I was unable to make this work in this case.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
% based on https://tex.stackexchange.com/a/34958/121799
\foreach \X [count=\Y] in {0,...,8}
{\pgfmathsetmacro{\myx}{\X*pi/4}
\ifnum\Y=1
\xdef\LstX{\myx}
\else
\xdef\LstX{\LstX,\myx}
\fi
}
\begin{tikzpicture}
\begin{axis}[domain=0:2*pi, ymax=1.2, samples=100, grid=major,
  xlabel=$\alpha$, ylabel=$\sin(\alpha)$, 
  xtick=\LstX,
      xticklabel={\pgfmathtruncatemacro{\tmp}{round(4*\tick/pi)}
      \pgfmathsetmacro{\mygcd}{gcd(\tmp,4)}%
      \pgfmathtruncatemacro{\mynumerator}{\tmp/\mygcd}%
      \pgfmathtruncatemacro{\mydenominator}{4/\mygcd}%
      \ifnum\mynumerator=0
       $\mathstrut\pgfmathprintnumber{0}$
      \else
       \ifnum\mynumerator=1
         \ifnum\mydenominator=1
          $\mathstrut\pi$       
         \else
          $\frac{\pi}{\pgfmathprintnumber{\mydenominator}}$     
         \fi
       \else
         \ifnum\mydenominator=1
          $\mathstrut\mynumerator\pi$       
         \else
          $\frac{\mynumerator\pi}{\pgfmathprintnumber{\mydenominator}}$
         \fi
       \fi
      \fi}, % https://tex.stackexchange.com/a/304032/121799
     ytick = {-1,-0.5,...,1}] %I assume this the part that I need to change?
    \addplot [blue](\x, {sin(deg(\x))});
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Update: The link I posted in the previous comment was a "dirty" escape for beginners like me ^^" but your answer is much more elegant and convenient. Thanks again! – Álvaro Sep 06 '18 at 23:37
  • @Álvaro Thanks, I guess it could be automatized much more. In principle, TikZ has very nice options, see e.g. on p. 949 of the pgfmanual the discussion on /pgf/number format/frac whole=true|false, however, numerical issues prevent me from using them... and even if you do xticklabel={\pgfkeys{/pgf/number format/.cd,frac, frac whole=false, /pgf/number format/frac shift=1} \pgfmathparse{\tick/pi} $\pgfmathprintnumber{\pgfmathresult}$, then you still get ugly things like 1/1... –  Sep 06 '18 at 23:42
  • Hmm I have however encountered an error while compiling. I am using Overleaf v1 and have received an error message saying: `Package PGF Math Error: Sorry, the operation 'gcd' has not yet b een implemented in the floating point unit (in 'gcd(0.0,4)').

    See the PGF Math package documentation for explanation. Type H for immediate help. ...

    l.41 \end{axis}` Any idea how I could fix this?

    – Álvaro Sep 06 '18 at 23:47
  • @Álvaro I do not get this error. However, I am wondering if it disappears if you replace gcd(\tmp,4) by gcd(int(\tmp),4). (I'm using an up-to-date version of TeXLive 2018.) –  Sep 06 '18 at 23:49
  • I am afraid that the error persists... I guess I'll have to do it the dirty way xD. Thanks a lot anyway – Álvaro Sep 06 '18 at 23:52
  • @Álvaro I actually should have used \pgfmathtruncatemacro{\tmp}{round(4*\tick/pi)} instead of \pgfmathsetmacro{\tmp}{round(4*\tick/pi)}, so my code worked by sheer luck on my machine. Does this replacement help you? –  Sep 06 '18 at 23:54
  • Sadly, it doesn't. I'm new to LaTeX, but I'm guessing that Overleaf v1 is just not up to date. – Álvaro Sep 06 '18 at 23:57
  • @Álvaro Sorry to hear that but I never used overleaf. All I know is that both round and \pgfmathtruncatemacro yield integers. I have been using the latter in many answers and in \ifnum conditions which are really allergic to 1.0, and have never received a complaint. Is there a way you could update your overleaf distribution? –  Sep 07 '18 at 00:01