Skill level: Beginner-friendly
What you’ll learn: Loops, functions, random colors, and Python’s built-in turtle
graphics module.
Outcome: A colorful spirograph pattern generator — readers can run it locally or in any Python environment that supports GUI windows.
A hypnotic, rainbow-colored spirograph made of circles overlapping at different angles.
import turtle
import colorsys
def draw_spirograph(size_of_gap, num_colors=36):
turtle.bgcolor("black")
turtle.speed("fastest")
turtle.hideturtle()
# Create a list of rainbow colors
colors = [colorsys.hsv_to_rgb(i/num_colors, 1, 1) for i in range(num_colors)]
colors = [(int(r*255), int(g*255), int(b*255)) for r, g, b in colors]
# Enable RGB mode
turtle.colormode(255)
for i in range(int(360 / size_of_gap)):
turtle.pencolor(colors[i % num_colors])
turtle.circle(100)
turtle.setheading(turtle.heading() + size_of_gap)
if __name__ == "__main__":
draw_spirograph(5)
turtle.done()
colorsys.hsv_to_rgb
turtle.colormode(255)
size_of_gap
degrees before drawing the next circle. This creates the spirograph effect.turtle.speed("fastest")
makes rendering almost instant.size_of_gap
to 10, 15, or 20 for different patterns.turtle.circle(100)
to a smaller or larger radius.num_colors
for more or fewer hues.If you want, I can also prepare: