6

I'm new to graphing on TikZ/pgf and have been trying to figure this out but can't seem to find any helpful info for what I want. I want to plot the above functions over one period, i.e. on the interval [0,2\pi] and would like the tick marks on the graph to show the values 0, pi/2 , pi, 3pi/2, and 2pi. Would appreciate some help.

2
  • Welcome to TeX-sx! Please help us help you by editing in a minimal working example of what you have tried. The problem seems relatively straight-forward with a little experience so I suspect there are particular areas we may need to focus on. – Joseph Wright Mar 3 '15 at 8:18
  • possible duplicate of Draw cosine and sine function with periods 1? – enthu Mar 3 '15 at 8:33
11

With pgfplots

\documentclass[border=4mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
     clip=false,
     xmin=0,xmax=2*pi,
     %axis lines=left,
     %axis x line=middle,
     %axis y line=left,
     xtick={0,1.57,3.14,4.71,6.28},
     xticklabels={$0$, $\frac{\pi}{2}$,$\pi$,$\frac{3}{2}\pi$,$2\pi$}
     ]
      \addplot[domain=0:2*pi,samples=200,red]{sin(deg(x))}node[right,pos=0.9]{$f(x)=\sin x$};
      \addplot[domain=0:2*pi,samples=200,blue]{cos(deg(x))}node[right,pos=0.9]{$f(x)=\cos x$};
    \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

Another variant will be:

\documentclass[border=4mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
     clip=false,
     xmin=0,xmax=2.5*pi,
     xlabel= $x$,
     ylabel=$f(x)$,
     ymin=-1.5,ymax=1.5,
     axis lines=middle,
     %axis x line=middle,
     %axis y line=left,
%     axis x line=middle,
     xtick={0,1.57,3.14,4.71,6.28},
     xticklabels={$0$, $\frac{\pi}{2}$,$\pi\,$,$\,\,\,\frac{3}{2}\pi$,$\,\,\,2\pi$},
     %xticklabel style={anchor=north west}
     ]
      \addplot[domain=0:2*pi,samples=200,red]{sin(deg(x))}
                                node[right,pos=0.9,font=\footnotesize]{$f(x)=\sin x$};
      \addplot[domain=0:2*pi,samples=200,blue]{cos(deg(x))}
                                node[right,pos=1,font=\footnotesize]{$f(x)=\cos x$};
    \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

2
  • Nice. The labels "f(x)=cos(x)" and "f(x)=sin(x)" would look better in smaller font, sitting beside the right endpoints of their graphs. – Benjamin McKay Mar 3 '15 at 7:12
  • Change node[right,pos=0.9]{$f(x)=\sin x$}; to node[right]{$\scriptstyle f(x)=\sin x$}; – Maarten Dhondt Mar 3 '15 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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