# Plot Fibonacci Sequence (with tikzpicture)

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]
\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}


• It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. – AML Jun 19 '18 at 14:45
• Since the sequence grows exponentially fast, I would think you would want to plot only the first few numbers. At which point, you may as well write the numbers in. – Teepeemm Jun 19 '18 at 18:47
• @Teepeemm I kinda arrived at that point as well... My current solution is to plot the discrete version of the Fibonacci function. I'll update the question. – DeeCeeDelux Jun 19 '18 at 19:04
• @TikzerWoods the formula generalizes to \sqrt(x^2 + 4) for F_n(x) that's why I wrote it - for optical reasons - as such in the code. – DeeCeeDelux Jun 19 '18 at 19:29
• @TikzerWoods I'll changed it to the 'golden ratio' formula for clarity. – DeeCeeDelux Jun 19 '18 at 19:38

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}{
...
}

• is there a way to get a dots instead of bars? – DeeCeeDelux Jun 19 '18 at 18:16
• @DeeCeeDelux You need only to replace  \fill[green,draw=black] (\n,0) rectangle +(1,\f); by \node[circle,fill] at (\n,\f){}; to get dots. (I am wondering, though, if there is a way to include 0 in the series.) – user121799 Jun 19 '18 at 18:29
• @marmot to start from 0 you can use \g in place of \f in the first two codes. And in the @jfbu's code you can use \xdef\fibs{0,\thexintfloatexpr rrseq(0, 1/10000 ; @1+@2, i=2..30)\relax}. – Kpym Jun 19 '18 at 19:18

The Fibonacci series is implemented in the pgfmanual in section 56.1. All you need to do is to adapt it from there.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{math}
\begin{document}

\tikzmath{
function fibonacci(\n) {
if \n == 0 then {
return 0;
} else {
return fibonacci2(\n, 0, 1);
}; };
function fibonacci2(\n, \p, \q) {
if \n == 1 then {
return \q;
} else {
return fibonacci2(\n-1, \q, \p+\q);
};
};
}
\begin{tikzpicture}
\foreach \X in {0,1,...,8}{
\node[circle,fill,label=above:{\pgfmathparse{int(fibonacci(\X))}
\pgfmathresult}] at (\X,{fibonacci(\X)})  {};}

\end{tikzpicture}
\end{document}


ADDITIONAL REMARK: It turns out not to be so easy to plot this function with pgfplots. The problem seems to be that pgfplots uses the fpu library (yet setting \pgfkeys{/pgf/fpu=false} does not help). In this answer, the problem is avoided by doing an external computation. However, I wrote the answer when there was no MWE available, and followed the directive "using tikzpicture preferably" ;-).

• @DeeCeeDelux The math library was introduced in version 3 of pgf/TikZ I believe, if you're still on version 2.x you will not have that library. You can check the version for example by placing \pgfversion in the document. – Torbjørn T. Jun 19 '18 at 15:28

With a variant of https://tex.stackexchange.com/a/51422/4427, we can generate the sequence to be fed to \foreach:

\documentclass[tikz]{standalone}
\usepackage{xparse}

\ExplSyntaxOn
\cs_new:Npn \fibo #1 { \fibo_recurrence:nnnn{0}{1}{0}{#1} }
\cs_new:Npn \fibo_recurrence:nnnn #1 #2 #3 #4
{
\int_compare:nTF { #1 = #4 }
{ #3 }
{
#3 , \fibo_recurrence:ffnn
{ \int_eval:n {#1+1} }
{ \int_eval:n {#2+#3} }
{ #2 }
{ #4 }
}
}
\cs_generate_variant:Nn \fibo_recurrence:nnnn { ffnn }
\ExplSyntaxOff

\begin{document}

\begin{tikzpicture}
\edef\fibos{\fibo{7}}
\foreach [count=\index] \f in \fibos
{
\fill[green,draw=black] (\index,0) rectangle +(1,\f)
node[black,scale=2,above left]{\f};
}
\end{tikzpicture}

\end{document}