Editor

Python Turtle Rainbow

Beginner · Nature · 47 lines · draws in about 10 seconds

A for loop draws seven half circles with a thick pen, each one 20 smaller than the one before, so the colours sit side by side in bands.

Python Turtle Rainbow: the finished drawing
import turtle
screen = turtle.Screen()
screen.bgcolor("skyblue")
screen.title("Rainbow")
t = turtle.Turtle()
t.speed(7)
def draw_cloud(x, y):
# A cloud is a few white dots that overlap
t.penup()
t.goto(x - 55, y)
t.dot(70, "white")
t.goto(x - 20, y + 25)
t.dot(85, "white")
t.goto(x + 25, y + 15)
t.dot(80, "white")
t.goto(x + 60, y)
t.dot(65, "white")
t.goto(x, y - 10)
t.dot(75, "white")
colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
# Seven half circles, one inside the other. Each band is 20 wide,
# so each circle is 20 smaller than the one before.
# steps=60 draws each half circle with 60 short lines, so it looks smooth.
t.pensize(20)
radius = 230
for color in colors:
t.penup()
t.goto(radius, -95)
t.setheading(90)
t.pendown()
t.pencolor(color)
t.circle(radius, 180, steps=60)
radius = radius - 20
# Clouds hide both ends of the rainbow
draw_cloud(-175, -110)
draw_cloud(175, -110)
t.hideturtle()
turtle.done()

How the code works

Clouds from dots

Five white dots of different sizes overlap to make one fluffy cloud. The function takes the cloud's position, so the same code draws both clouds.

def draw_cloud(x, y):
    # A cloud is a few white dots that overlap
    t.penup()
    t.goto(x - 55, y)
    t.dot(70, "white")
    t.goto(x - 20, y + 25)
    t.dot(85, "white")
    t.goto(x + 25, y + 15)
    t.dot(80, "white")
    t.goto(x + 60, y)
    t.dot(65, "white")
    t.goto(x, y - 10)
    t.dot(75, "white")

Seven half circles

For each colour the turtle goes to the right end of the arc, points up with setheading(90) and draws circle(radius, 180): half a circle, curving left over the top. Then radius drops by 20, the width of the pen, so the next band fits inside.

# Seven half circles, one inside the other. Each band is 20 wide,
# so each circle is 20 smaller than the one before.
# steps=60 draws each half circle with 60 short lines, so it looks smooth.
t.pensize(20)
radius = 230
for color in colors:
    t.penup()
    t.goto(radius, -95)
    t.setheading(90)
    t.pendown()
    t.pencolor(color)
    t.circle(radius, 180, steps=60)
    radius = radius - 20

Clouds hide the ends

The ends of the thick arcs are round blobs. The clouds are drawn last, and whatever is drawn later sits on top, so they cover the ends.

# Clouds hide both ends of the rainbow
draw_cloud(-175, -110)
draw_cloud(175, -110)

Make it yours

Change the code above and press Run. Some ideas:

  • Change t.pensize(20) to t.pensize(10) and radius - 20 to radius - 10 for thin bands.
  • Use pastel colours: colors = ["lightcoral", "peachpuff", "lightyellow", "palegreen", "lightblue", "plum", "thistle"].
  • Delete the two draw_cloud lines and change 180 to 360 in t.circle(radius, 180, steps=60) to see whole rings, the way a rainbow looks from a plane.

What it uses

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