In this post, we will understand How to Draw a Rainbow in Python Turtle graphic with examples. To use a Turtle library make sure it’s installed on the local system or else install it using the command “pip install PythonTurtle” once installed import in our program using import Turtle
-Python Turtle code to Draw Rainbow---
import turtle
import colorsys
window = turtle.setup(width=400,height=450)
myturtle = turtle.Turtle()
myturtle.speed(0)
myturtle.hideturtle()
turtle.bgcolor('light blue')
turtle.title('49-Color Rainbow')
n_colors = 49
def draw_arch(x,y,r,pensize,color):
myturtle.up()
myturtle.goto(x+r,y)
myturtle.down()
myturtle.seth(90)
myturtle.pensize(pensize)
myturtle.pencolor(color)
myturtle.circle(r,180)
radius = 150
penwidth = 20*7/n_colors
hue = 0
for i in range(n_colors):
(red,green,blue) = colorsys.hsv_to_rgb(hue,1,1)
draw_arch(0,-100,radius,penwidth,(red,green,blue))
radius -= (penwidth-1)
hue += 0.9/n_colors
Output
