Editor

Hexagon Spiral in Python Turtle

Intermediate · Patterns and spirals · 32 lines · draws in about 16 seconds

Every hexagon starts at the same corner, a little bigger and turned 5 degrees further than the last, so 72 of them curl into a rainbow shell.

Hexagon Spiral in Python Turtle: the finished drawing
import turtle
import colorsys
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("Hexagon spiral")
t = turtle.Turtle()
t.speed(10)
t.pensize(2)
def draw_hexagon(side):
for i in range(6):
t.forward(side)
t.left(60)
# Start a little left and down, so the finished spiral sits in the middle
t.penup()
t.goto(-65, -32)
t.pendown()
# 72 hexagons, each a little bigger and turned 5 degrees (72 x 5 = 360)
for i in range(72):
hue = i / 72
t.pencolor(colorsys.hsv_to_rgb(hue, 0.9, 1))
draw_hexagon(7 + i * 1.9)
t.left(5)
t.hideturtle()
turtle.done()

How the code works

A function for one hexagon

Six equal lines with a 60 degree turn after each make a hexagon, and the turtle ends where it started, facing the same way. side sets how big it is.

def draw_hexagon(side):
    for i in range(6):
        t.forward(side)
        t.left(60)

Start off-centre

Every hexagon grows up and to the right of the corner it starts from. Starting at (-65, -32) puts the middle of the finished shell in the middle of the screen.

# Start a little left and down, so the finished spiral sits in the middle
t.penup()
t.goto(-65, -32)
t.pendown()

Bigger, turned and recoloured

hue goes from 0 to almost 1, and colorsys.hsv_to_rgb() turns it into a colour of the rainbow, from red through yellow, green and blue to purple. Each side is 7 + i * 1.9 steps, so every hexagon is bigger than the last, and left(5) turns the next one.

# 72 hexagons, each a little bigger and turned 5 degrees (72 x 5 = 360)
for i in range(72):
    hue = i / 72
    t.pencolor(colorsys.hsv_to_rgb(hue, 0.9, 1))
    draw_hexagon(7 + i * 1.9)
    t.left(5)

Make it yours

Change the code above and press Run. Some ideas:

  • Change the 0.9 in hsv_to_rgb(hue, 0.9, 1) to 0.4 for soft pastel colours.
  • Inside draw_hexagon, change range(6) to range(4) and left(60) to left(90) to build the shell from squares.
  • Change t.left(5) to t.left(15) and the hexagons spread into a lacy swirl that goes round three times.

What it uses

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