Contents

Drawing a Sunflower Pattern (Phyllotaxis) with TikZ in LaTeX

This article presents an aesthetic analysis and a detailed mathematical breakdown of how to draw a sunflower (phyllotaxis) pattern using the TikZ package in LaTeX.


Phyllotaxis (or phyllotaxy) is the arrangement of leaves, petals, or seeds on a plant. One of the most fascinating phenomena in nature is the spiral pattern found on a sunflower head. This pattern is not random; it follows precise mathematical rules involving the Golden Ratio and the Golden Angle.

In LaTeX-based graphics, we can reproduce this natural beauty with high precision using the TikZ package. This article explores a TikZ implementation by David Convent, which adapts Jim Bumgardner’s tutorial “Circles, Spirals and Sunflowers”. We will dissect how mathematical logic is translated into LaTeX code to generate a stunning phyllotaxis visualization.


Here is the complete LaTeX code to generate the phyllotaxis pattern using TikZ:

% Phyllotaxy
% Author: David Convent
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{calc}

\def\nbrcircles {377}
\def\outerradius {30mm}
\def\deviation {.9}
\def\fudge {.62}

\newcounter{cumulArea}
\setcounter{cumulArea}{0}

\begin{document}
\begin{tikzpicture}[scale=.32]
  \pgfmathsetmacro {\goldenRatio} {(1+sqrt(5))}
  \pgfmathsetmacro {\meanArea} {pow(\outerradius * 10 / \nbrcircles, 2) * pi}
  \pgfmathsetmacro {\minArea} {\meanArea * (1 - \deviation)}
  \pgfmathsetmacro {\midArea} {\meanArea * (1 + \deviation) - \minArea}
  
  \foreach \b in {0,...,\nbrcircles}{
    % mod() must be used in order to calculate the right angle.
    % otherwise, when \b is greater than 28 the angle is greater
    % than 16384 and an error is raised ('Dimension too large').
    % -- thx Tonio for this one.
    \pgfmathsetmacro{\angle}{mod(\goldenRatio * \b, 2) * 180}
    \pgfmathsetmacro{\sratio}{\b / \nbrcircles}
    \pgfmathsetmacro{\smArea}{\minArea + \sratio * \midArea}
    \pgfmathsetmacro{\smRadius}{sqrt(\smArea / pi) / 2 * \fudge}
    \addtocounter{cumulArea}{\smArea};
    \pgfmathparse{sqrt(\value{cumulArea} / pi) / 2}
    \fill[] (\angle:\pgfmathresult) circle [radius=\smRadius] ;
  }
\end{tikzpicture}
\end{document}

When compiled, the code above yields the following spiral pattern:

Sunflower Pattern (Phyllotaxis)
Sunflower Pattern (Phyllotaxis)


\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{calc}
  • standalone: This document class ensures that the output PDF or image matches the bounding box of the drawn objects exactly, without default page margins.
  • calc: A TikZ library that enables coordinate calculations and advanced mathematical expressions.
\def\nbrcircles {377}
\def\outerradius {30mm}
\def\deviation {.9}
\def\fudge {.62}
  • \nbrcircles: The total number of circles (seeds) to draw. The number 377 is a Fibonacci number, which naturally appears in plant spiral patterns.
  • \outerradius: The outer boundary radius of the overall design (30 mm30\text{ mm}).
  • \deviation: A value (0.90.9) controlling how quickly the seed circles grow in size as they move outward.
  • \fudge: A scaling factor (0.620.62) to pack or space out the circles nicely without excessive overlapping.
\newcounter{cumulArea}
\setcounter{cumulArea}{0}

Defines a LaTeX counter named cumulArea to track the sum of the areas of all circles drawn so far. This counter helps calculate the radial distance of each circle from the center.

\pgfmathsetmacro {\goldenRatio} {(1+sqrt(5))}
\pgfmathsetmacro {\meanArea} {pow(\outerradius * 10 / \nbrcircles, 2) * pi}
\pgfmathsetmacro {\minArea} {\meanArea * (1 - \deviation)}
\pgfmathsetmacro {\midArea} {\meanArea * (1 + \deviation) - \minArea}
  • \goldenRatio: Defined as 1+51 + \sqrt{5}, which equals 2Φ2\Phi (twice the Golden Ratio).
  • \meanArea: The average area of each small circle.
  • \minArea and \midArea: Lower bounds and scaling parameters to grow the circle size linearly from the center outwards.
\foreach \b in {0,...,\nbrcircles}{
  \pgfmathsetmacro{\angle}{mod(\goldenRatio * \b, 2) * 180}

This is where the mathematical magic happens. Each seed is placed at an angle that is a multiple of the Golden Angle.

  • The expression mod(\goldenRatio * \b, 2) * 180 multiplies the seed index \b by 2Φ2\Phi, takes the modulo 22, and scales the result (in the range [0,2)[0, 2)) by 180180^\circ to get a degree angle in [0,360)[0^\circ, 360^\circ).
  • The use of mod() is crucial. Without it, the cumulative angle would exceed TeX’s internal coordinate limit of 1638416384 after just 28 seeds, throwing a “Dimension too large” error.
  \pgfmathsetmacro{\sratio}{\b / \nbrcircles}
  \pgfmathsetmacro{\smArea}{\minArea + \sratio * \midArea}
  \pgfmathsetmacro{\smRadius}{sqrt(\smArea / pi) / 2 * \fudge}
  • \sratio: The ratio of the current seed’s index to the total number of seeds (ranging from 00 at the center to 11 at the boundary).
  • \smArea: Calculates the area of the current seed linearly. Outer seeds have larger areas than inner ones.
  • \smRadius: Converts the area back to a circle radius (r=Area/πr = \sqrt{\text{Area}/\pi}) and scales it by \fudge for aesthetic spacing.
  \addtocounter{cumulArea}{\smArea};
  \pgfmathparse{sqrt(\value{cumulArea} / pi) / 2}
  \fill[] (\angle:\pgfmathresult) circle [radius=\smRadius] ;
}
  • \addtocounter{cumulArea}{\smArea}: Adds the current seed’s area to the running total.
  • \pgfmathparse{...}: Calculates the radial distance from the center as the square root of the cumulative area divided by π\pi. This ensures a perfectly uniform distribution density across the entire spiral.
  • \fill[]: Draws a black circle at polar coordinates (\angle:\pgfmathresult) with the radius \smRadius.

The generated pattern highlights nature’s geometry:

  • The Golden Angle (137.5137.5^\circ): This irrational angle is the most efficient way to partition circular space. It prevents the seeds from aligning in straight radial lines, avoiding gaps.
  • Fibonacci Sequences: The clockwise and counter-clockwise spirals on a sunflower head always form consecutive Fibonacci numbers (e.g., 34 and 55, or 55 and 89). Drawing 377377 circles (the 14th Fibonacci number) honors this natural symmetry.
  • Constant Packing Density: By using the cumulative area to determine radial coordinates, the seed density remains constant from the core to the edges, creating a highly balanced layout.

Try modifying these parameters for interesting visual variations:

  1. Adjust the Seed Count: Change \nbrcircles {377} to other Fibonacci numbers like 144 (sparser) or 987 (very dense).
  2. Apply Color Gradients: Instead of solid black (\fill[]), color each circle based on its index \b:
    \definecolor{mycolor}{Hsb}{\b*0.8, 0.8, 0.9}
    \fill[mycolor] (\angle:\pgfmathresult) circle [radius=\smRadius];
  3. Change the Scaling Factor (\fudge): Adjust \fudge {.62} to 0.8 to overlap the circles into a tight mosaic, or down to 0.4 to introduce open space between them.

David Convent’s TikZ code demonstrates how simple mathematical rules can model complex organic structures. Through the Golden Angle and cumulative area calculations, LaTeX allows us to create beautiful botanical designs with absolute precision.


  1. David Convent, Sunflower pattern (Phyllotaxy), TeXample.net
  2. Jim Bumgardner, Circles, Spirals and Sunflowers, krazydad.com
  3. Till Tantau, The TikZ and PGF Manual, v3.1.10, 2024.
  4. StackExchange discussion, Dimension too large error with TikZ mod(), tex.stackexchange.com/questions/192028/

Written by: Aan Triono
License: CC BY-SA 4.0

Related Content