Editor

Sunflower in Python Turtle

Intermediate · Nature · 45 lines · draws in about 17 seconds

Each seed turns 137.5 degrees from the one before and sits a little further out, and that one rule packs 200 seeds into the spirals of a real sunflower.

Sunflower in Python Turtle: the finished drawing
import turtle
import math
screen = turtle.Screen()
screen.bgcolor("skyblue")
screen.title("Sunflower")
t = turtle.Turtle()
t.speed(10)
def draw_petal(size):
# Two long arcs make a thin pointed petal that starts at the centre
t.color("darkorange", "gold")
t.begin_fill()
t.right(23) # half of 46, so the petal points straight ahead
t.circle(size, 46)
t.left(134) # 180 - 46: turn round at the tip
t.circle(size, 46)
t.left(157) # 134 + 23: face the way the turtle started
t.end_fill()
# 24 petals all the way round
for i in range(24):
draw_petal(270)
t.left(15)
# A brown disc covers the inner ends of the petals
t.dot(290, "saddlebrown")
# Seeds: each one turns by the golden angle (about 137.5 degrees, the turn
# real sunflowers use) and sits a little further out than the one before
GOLDEN_ANGLE = 137.508
t.penup()
for i in range(200):
angle = math.radians(i * GOLDEN_ANGLE)
distance = 9.5 * math.sqrt(i)
x = distance * math.cos(angle)
y = distance * math.sin(angle)
t.goto(x, y)
t.dot(12, "#3b2008")
t.hideturtle()
turtle.done()

How the code works

Thin petals

Two long, shallow arcs of 46 degrees make a thin petal that starts at the centre of the flower. The turns at the start and the end keep it pointing the way the turtle faced.

def draw_petal(size):
    # Two long arcs make a thin pointed petal that starts at the centre
    t.color("darkorange", "gold")
    t.begin_fill()
    t.right(23)          # half of 46, so the petal points straight ahead
    t.circle(size, 46)
    t.left(134)          # 180 - 46: turn round at the tip
    t.circle(size, 46)
    t.left(157)          # 134 + 23: face the way the turtle started
    t.end_fill()

Petals all the way round

24 petals, 15 degrees apart, fill the circle. A big brown dot() then covers their inner ends and becomes the seed head.

# 24 petals all the way round
for i in range(24):
    draw_petal(270)
    t.left(15)

# A brown disc covers the inner ends of the petals
t.dot(290, "saddlebrown")

Seeds by the golden angle

Seed number i sits at an angle of i * GOLDEN_ANGLE and a distance of 9.5 * math.sqrt(i) from the centre. math.cos() and math.sin() turn that angle and distance into x and y, and goto() takes the turtle there to drop a dot. The square root spreads the seeds evenly, so the head fills without gaps.

# Seeds: each one turns by the golden angle (about 137.5 degrees, the turn
# real sunflowers use) and sits a little further out than the one before
GOLDEN_ANGLE = 137.508
t.penup()
for i in range(200):
    angle = math.radians(i * GOLDEN_ANGLE)
    distance = 9.5 * math.sqrt(i)
    x = distance * math.cos(angle)
    y = distance * math.sin(angle)
    t.goto(x, y)
    t.dot(12, "#3b2008")

Make it yours

Change the code above and press Run. Some ideas:

  • Change GOLDEN_ANGLE = 137.508 to GOLDEN_ANGLE = 137.0 and the seeds line up in curved arms with gaps between them.
  • Change t.dot(12, "#3b2008") to t.dot(8, "#3b2008") for smaller seeds that show the spirals more clearly.
  • Change "gold" in draw_petal to "orangered" for a red sunflower.

What it uses

Every turtle command the site supports is listed on Python turtle online.