Home › Code art › Lesson 1
Sunflower ka secret: 137.5°
Code art with Raj, notes 1. Draw a sunflower head with 15 lines of Python, then break it on purpose. Free, simple English.
The idea
Put dot number 1, then dot number 2, then dot number 3, and so on. Each new dot goes a little further from the centre, and turns by the same angle from the dot before it. That is the whole recipe.
The only thing you choose is that angle. With 137.5° (the "golden angle") the dots spread out evenly and you get a sunflower-like head with spirals in it. With other angles, the picture changes a lot. Run it and see.
Important: this is a model of a sunflower head, not the exact formula of a real flower. Real sunflowers are only roughly like it. The model is called the Vogel spiral (see sources).
The code: sunflower.py
You need Python and Pillow (pip install pillow). Nothing else. The code saves pictures into a folder called frames, so make that folder first, in the same place as the file: mkdir frames. The file has 15 real code lines, plus comments.
# Sunflower from the golden angle (our own code, Pillow only). Vogel-style model: dot i sits at radius sqrt(i), angle i * ANGLE.
import math
from PIL import Image, ImageDraw, ImageFilter, ImageChops
def sunflower(angle=137.50776, n=800, size=1080, name="sunflower"):
dots = Image.new("RGB", (size, size), "black")
d = ImageDraw.Draw(dots)
for i in range(1, n + 1):
r = math.sqrt(i) * size * 0.0175 # distance grows slowly
a = math.radians(i * angle) # each dot turns by the same angle
x, y = size / 2 + r * math.cos(a), size / 2 + r * math.sin(a)
t = i / n
d.ellipse((x - 9, y - 9, x + 9, y + 9), fill=(255, int(150 + 80 * t), int(40 + 90 * t)))
glow = ImageChops.add(dots, dots.filter(ImageFilter.GaussianBlur(14))) # soft glow
glow.save(f"frames/{name}.png")
for ang in (137.50776, 137.0, 138.0, 90.0):
sunflower(ang, name=f"sunflower-{ang}")
Run it: python sunflower.py. It saves four pictures in frames, one for each angle in the for line at the bottom: 137.50776, 137.0, 138.0 and 90.0.
How it works, line by line
r = math.sqrt(i) * ...: dot numberisits at a distance of about the square root ofi. The distance grows slowly, so the dots fill a round head.a = math.radians(i * angle): dot numberihas turneditimes the angle. Same turn every time.x, y = ...: turns that distance and direction into a spot on the picture.- The colour goes from orange to yellow as
tgrows (the later dots are yellower). - The last lines of the function blur a copy and add it back. That gives the soft glow.
The experiment: change one number
We drew the same 800 dots four times. Only the angle changed.




What is 137.5°, really?
The golden angle is about 137.5077640500°. It comes from 360 × (1 − 1/φ), where φ (phi) is the golden ratio, (1 + √5) / 2. "137.5" is a rounded number. We checked this with Python: the arithmetic gives 137.507764.
Careful: we have not shown here that the golden angle is proven to give the best possible packing. We only show what the picture looks like in this model.
Try it yourself
- Add more angles to the
forline at the bottom, for example(137.5, 137.6, 137.4). How different do they look? - Change
n=800to 300, then 2000. What changes in the picture? - Try an angle of 120 or 72. What shape do you get? (Think: how many times does it fit in 360?)
Remember
One small number, the angle, decides the whole picture. Real flowers are more complicated, but this small model shows why 137.5° is special.
Sources
- Vogel spiral, Wolfram MathWorld: mathworld.wolfram.com/VogelSpiral.html
- Vogel, H. (1979), "A better way to construct the sunflower head", Mathematical Biosciences 44(3-4):179-189, DOI 10.1016/0025-5564(79)90080-4
- Golden angle, Wikipedia: en.wikipedia.org/wiki/Golden_angle
The code and pictures on this page are our own. Next: Neon titli: ek equation →