[Tex/LaTex] Plot Fibonacci Sequence (with tikzpicture)

tikz-pgf

How do I plot the Fibonacci sequence (using tikzpicture preferably)?

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[xmin=0, xmax=30, ymin=2, ymax=3]
    \addplot[samples at={1,2,...,30}, only marks] expression {<Add Fibonacci sequence here>};
  \end{axis}
\end{tikzpicture}

\end{document}

Edit

Since it seems more complicated than I thought, I went for the following solution: plot the discrete version of the continuous Fibonacci function, see for instance here. Any suggestions are surely still welcomed.

So now it goes like this:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[xmin=0, xmax=8, ymin=0, ymax=14, xlabel=$n$, ylabel=$a_n$, axis x line=center, axis y line=center]
    \addplot[samples at={0,1,...,7},only marks] expression {( ((1+sqrt(5))/(2))^\x - cos(deg(\x * pi)) * ((1+sqrt(5))/(2))^(-\x) )/sqrt(5) };
  \end{axis}
\end{tikzpicture}

\end{document}

Fibonacci function

Best Answer

Here is a direct loop method :

\documentclass[tikz,border=7pt]{standalone}
\begin{document}
  \tikz
    \foreach[
        remember=\g as \h (initially 1),
        remember=\f as \g (initially 0),
        evaluate=\f using int(\g+\h)
      ] \n in {1,...,7}
      \fill[green,draw=black] (\n,0) rectangle +(1,\f) node[black,scale=2,above left]{\f};
\end{document}

EDIT: If you want to go to 30 (and more) you can use xint package.

\documentclass[tikz,border=7pt]{standalone}
\usepackage{xintexpr}
\begin{document}
  \begin{tikzpicture}[xscale=.35]
    \foreach[
        remember=\g as \h (initially 1),
        remember=\f as \g (initially 0)
      ] \n in{1,...,30}{
        \edef\f{\thexintexpr \g + \h \relax}
        \edef\ff{\thexintfloatexpr \f/10000 \relax}
        \fill[green,draw=black] (\n,0) rectangle +(1,\ff);
      }
  \end{tikzpicture}
\end{document}

EDIT: Following the comment of @jfbu the following code will produce the same image:

\documentclass[tikz,border=7pt]{standalone}
\usepackage{xintexpr}
\begin{document}
  \xdef\fibs{\thexintfloatexpr rrseq(0, 1/10000 ; @1+@2, i=2..30)\relax}
  \tikz[xscale=.35]
    \foreach[count=\n] \f in \fibs
      \fill[green,draw=black] (\n,0) rectangle +(1,\f);
\end{document}

NOTE: It is probably faster to use directly something like

\foreach[count=\n] \f in {1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040}{
  ...
}