10

i would like to lable my x-axis on a tikz-picture in π/4 increments, like in the wikipedia sin and cos graphic 1. This is my code (but it doesnt work):

\documentclass{scrartcl}
\usepackage{lmodern} % Schriftart
\usepackage[utf8]{inputenc} % Umlaute im Quellcode benutzen
\usepackage[T1]{fontenc} % Umlaute korrekt darstellen
\usepackage[ngerman]{babel} % Worttrennung
\usepackage[landscape, margin=20mm]{geometry}
\usepackage{blindtext}

\usepackage{tikz}

\pagestyle{empty} \setlength\parindent{0mm}

\begin{document} \blindtext

\begin{tikzpicture}[scale=2, domain=0:{2*pi}]
    %\clip (-0.1,-0.2) rectangle (1.1,0.75); %Bildausschnitt
    \draw[step=0.5,gray,very thin] (0,-1.4) grid (6.4,1.4); %Hintergrundgitter
    \draw[->] (0,0) -- (6.5,0) node[right]{$x$}; %x-Achse
    \draw[->] (0,-1.5) -- (0,1.5) node[above]{$y$}; %y-Achse

    \foreach \x in {0\pi,0.5\pi,...\pi,2\pi}{$\x$}
        \draw (\x,2pt) -- (\x,-2pt) node[below]{$\x$};

    \foreach \y in {-1,-0.5, ...,1} 
        \draw(2pt,\y) -- (-2pt,\y) node[left]{$\y$};

    \draw[color=red, very thick, smooth] plot (\x,{sin(\x r)});
    \draw[color=blue, very thick, smooth] plot (\x,{cos(\x r)});
    %\draw[smooth, variable=\x, blue] (0.0) plot ({\x}, {sin(\x r)});
\end{tikzpicture}

\end{document}

F. Pantigny
  • 40,250
finpe
  • 103

3 Answers3

12

If you really want to stick with tikz (although I would recommend you use pgfplots for graphing),you can:

  1. Use \pgfmathsetmacro to compute the appropriate x-axis location as a multiple of pi.
  2. Use \pgfmathprintnumber to format the number with the number format set to frac.
  3. Detect the case of 1 (so you don't end up with a label of 1\pi).

Notes:

References:

  • If you are creating several graphs, I would recommend to switch to using the axis environment from the pgfplots package which is specifically designed for graphs. An example of its use with trigonometric graphs is at Axis with trigonometric labels in PGFPlots.

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fpu}% needed for for the 'frac' detection

\NewDocumentCommand{\FormatMultipleOfGivenValue}{s O{} m}{% %% #1 = * to suppress the display of "0" %% #2 = multiplier to display %% #3 = real number \pgfmathifisint{#3}{% \pgfmathparse{int(#3)}% \ifnum\pgfmathresult=0 \IfBooleanF{#1}{0}% Suppress "0" if * option provided. \else \ifnum\pgfmathresult=1\relax %% https://tex.stackexchange.com/q/53068 \ifx#2\pi\relax\pi\else1\fi \else \pgfmathresult#2 \fi \fi }{% \pgfmathprintnumber{#3}#2 }% }% \begin{document} \begin{tikzpicture}[scale=2, domain=0:{2*pi}] \pgfkeys{/pgf/number format/.cd, frac, frac whole=false} \draw [xstep=0.25*pi, ystep=0.5, gray,very thin] (0,-1.0) grid (2*pi,1.0); \draw [-latex] (0,0) -- (6.5,0) node [right] {$x$}; %x-Achse \draw [-latex] (0,-1.3) -- (0,1.3) node [above] {$y$}; %y-Achse

    \foreach \x in {0.0, 0.5,..., 2, 6.5}{
        \pgfmathsetmacro\xCoord{\x*pi}
        \draw (\xCoord,2pt) -- (\xCoord,-2pt) node [below] 
            {$\FormatMultipleOfGivenValue*[\pi]{\x}$};
    }

    \foreach \y in {-1,-0.5, ...,1} {
        \draw (2pt,\y) -- (-2pt,\y) node [left] {$\FormatMultipleOfGivenValue{\y}$};
    }

    \draw[color=red,  ultra thick, smooth] plot (\x,{sin(\x r)});
    \draw[color=blue, ultra thick, smooth] plot (\x,{cos(\x r)});
\end{tikzpicture}

\end{document}

Peter Grill
  • 223,288
  • Hello Peter, thank you very much for your help witch my first question on stackexchange! Your solution seems easy and reasonable, i will implement it! – finpe Mar 01 '22 at 09:06
  • @infinitezero: Thanks for catching that -- has been corrected. – Peter Grill Mar 01 '22 at 17:24
8

Try this code:

\documentclass[margin=.5cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[>=latex,scale=1.5]
    % first option for a grid like a millimetral grahic paper (next 3 lines)
    \draw[thin,brown!25] (-.5,-1.5) grid[step= 1mm] ++ (7.5,3);
    \draw[semithick,brown!50]   (-.5,-1.5) grid[step= 5mm] ++  (7.5,3);
    \draw[    thick,brown!75]   (-.5,-1.5) grid[step=10mm] ++  (7.5,3);

    % second option for a grid in dotted gray in next line (uncomment the next line but comment the preceding 3 lines)
    %\draw[dotted] (-0,-1) grid (6.5,1);

    % x axis and its label
    \draw[->] (-.5,0) -- (6.5,0) node[right] {$x$};

    % x axis graduations in integers 
    \foreach \x in {1,2,...,6}
    \fill[shift={(\x,0)}] circle(.5pt) node[above] {\tiny $\x$};

    % x axis graduations in \pi
    \foreach \x /\n in {.7854/$\frac{\pi}{4}$,1.5708/$\frac{\pi}{2}$,2.3562/$\frac34\pi$,3.1416/$\pi$,3.9270/$\frac54\pi$,4.7124/$\frac32\pi$,5.4978/$\frac74\pi$,6.2832/$2\pi$} \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {\tiny \n};

    % y axis, its label and graduations
    \draw[->] (0,-1.5) -- (0,1.5) node[below right] {$y$};
    \foreach \y in {-1,1}
    \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {\tiny $\y$};
    \node[below left] at (0,0) {\tiny $0$};

    % draw functions
    \draw[smooth,blue,mark=none,domain=0:6.283,line width=1.5pt] plot (\x,{sin(deg(\x))});
    \draw[smooth,magenta,mark=none,domain=-0:6.283,line width=1.5pt] plot (\x,{cos(deg(\x))});

    % draw the functions names
    \draw[] (2,-1) node[magenta,below](1) {$y=\cos x$};
    \draw[] (2,1) node[blue,above](1) {$y=\sin x$};

    % plot the intersections points A and B
    \fill[black,shift={(3.9270,-.707)}] circle(1.5pt) node[below] {\tiny B};
    \fill[black,shift={(.7854,.707)}] circle(1.5pt) node[above] {\tiny A};

    % draw the exact coordinates of A and B
    \draw[] (5,-.95) node[below] (3) {\tiny A$\left(\frac{\pi}{4},\frac{\sqrt2}{2}\right)$ \quad \tiny B$\left(\frac{5}{4}\pi,-\frac{\sqrt2}{2}\right)$};
\end{tikzpicture}

\end{document}

enter image description here

5

Yet another way using the tikz library datavisualization.formats.functions

\documentclass{standalone}

\usepackage{tikz} \usetikzlibrary {datavisualization.formats.functions}

\begin{document} \begin{tikzpicture}[] \datavisualization [school book axes, visualize as smooth line/.list={sin,cos}, sin={style={red,very thick}, label in legend={text=$\sin(x)$}}, cos={style={blue,very thick},label in legend={text=$\cos(x)$}}, all axes={ticks={step=0.5}}, x axis={grid={step=0.25}, length=13cm, ticks={tick unit=\pi}, label=$x$, max value=2.25}, y axis={grid,length=6cm,label=$y$,max value=1.25} ] data [set=sin,format=function] { var x : interval [0:2]; func y = sin(\value x * pi r); } data [set=cos,format=function] { var x : interval [0:2]; func y = cos(\value x * pi r); }; \end{tikzpicture}

\end{document}

enter image description here