5

My question

How can I add plots to the graph I am drawing, in order to have a more precise drawing?


My code

I am learning how to plot graphs.

Here is the code I use. You can see on the picture that the curve is not smooth: one should use more plots to draw this graph.

\begin{tikzpicture}[domain=-5:8]
\draw[very thin,color=gray] (-5.1,-1.6) grid (8.1,1.6);
\draw[->] (-5.2,0) -- (8.2,0) node[right] {$x$};
\draw[->] (0,-1.7) -- (0,1.7) node[right] {$\sin(x)$};
\draw[color=orange] plot function{sin(x)} ;
\end{tikzpicture}

enter image description here

8

You can add smooth as the option.

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
  \begin{tikzpicture}[domain=-5:8]
\draw[very thin,color=gray] (-5.1,-1.6) grid (8.1,1.6);
\draw[->] (-5.2,0) -- (8.2,0) node[right] {$x$};
\draw[->] (0,-1.7) -- (0,1.7) node[right] {$\sin(x)$};
\draw[color=orange] plot[smooth] function{sin(x)} ;
\end{tikzpicture}
\end{document}

enter image description here

With powerful pgfplots, there is a samples key using which you can add more data points to be plotted

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{tikzpicture}

\begin{axis}[xmin=0,xmax=10,
    ymin=-2,ymax=2,grid=major,
    ]    
    \addplot[domain=0:4*pi,samples=500]  {sin(deg(x))};   %% change samples value. you can also add smooth option
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

With more samples plots become more smooth at the cost of compilation time. Some times it is advisable to use gnuplot from within addplot as gnuplot is better in doing computations.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{tikzpicture}

\begin{axis}[xmin=0,xmax=10,
    ymin=-2,ymax=2,grid=major,
    ]    
    \addplot[blue, samples=5000] gnuplot[domain = 0:4*pi] {sin(x)};   %% change samples value.
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

pgfplots offers lot of goodies for plotting. I suggest you switch to it for plotting needs.

As suggested by Claudio Fiandrino, you can use the versatile hobby tikz library. There are two options - quick hobby and hobby` for open curves.

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{hobby}
\begin{document}
  \begin{tikzpicture}[domain=-5:8]
\draw[very thin,color=gray] (-5.1,-1.6) grid (8.1,1.6);
\draw[->] (-5.2,0) -- (8.2,0) node[right] {$x$};
\draw[->] (0,-1.7) -- (0,1.7) node[right] {$\sin(x)$};
\draw[color=orange] plot[quick hobby] function{sin(x)} ;    % quick hobby or simply hobby
\end{tikzpicture}
\end{document}

enter image description here

11
  • Or even hobby... Apr 4 '14 at 14:00
  • Great.. looking for the complete answer, but don't expect another upvote from me: already done ;) Apr 4 '14 at 14:05
  • Yes, I had in mind \draw[color=orange] plot[hobby] function{sin(x)} ;.. Apr 4 '14 at 14:25
  • Samples is TikZ option.
    – percusse
    Apr 4 '14 at 14:27
  • 1
    @Colas If for tikz add x=<dimension> to the option of tikzpicture. For example, \begin{tikzpicture}[x=0.5cm]` will reduce width by 50% (x=2cm will double the width). For pgfplots add width=<dimension> to the axis options → \begin{axis}[width=2in,xmin=0,...]
    – user11232
    Apr 5 '14 at 12:37
6

You can change to pgfplots

\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
\begin{axis}[axis lines=center,domain=-5:8, xlabel=$x$, ylabel=$y$]
\addplot[orange,smooth] {sin(deg(x))} ;
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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.