Draw Rainbow In Python Using Turtle
In this tutorial we will see how to draw rainbow using python, we will use the turtle module to create this program in python, It is a GUI python library which can be used to draw anything from characters, cartoons, shapes and other objects.
Python Code To Draw Rainbow
import turtle import colorsys def draw_one_color_arc(x,y,r,pensize,color): turtle.up() turtle.goto(x+r,y) turtle.down() turtle.seth(90) turtle.pensize(pensize) turtle.pencolor(color) turtle.circle(r,180) turtle.speed(0) turtle.hideturtle() turtle.bgcolor('light blue') turtle.title('Rainbow In Python Turtle') turtle.setup(700,700) num_colors = 49 radius = 300 penwidth = 20*7/num_colors hue = 0 for i in range(num_colors): (r,g,b) = colorsys.hsv_to_rgb(hue,1,1) draw_one_color_arc(0,-100,radius,penwidth,(r,g,b)) radius -= (penwidth-1) hue += 0.9/num_colors turtle.done()
Above is the code for drawing rainbow, let’s see how this code actually works:
draw_one_color_arcfunction is defined to draw a single arc of a specific color at a given position.
- The turtle module is initialized with various settings such as speed, background color, and window size.
- The variable
num_colorsis set to 49, indicating the number of colors that will be used to draw the rainbow.
radiusvariable is set to 300, which represents the initial radius of the largest arc.
penwidthvariable is calculated based on the number of colors, and it determines the width of each arc.
- The code uses the
colorsysmodule to convert the hue value (ranging from 0 to 1) to RGB values. The hue value gradually changes for each arc to create a rainbow effect.
- A loop is used to draw each arc. Inside the loop, the
draw_one_color_arcfunction is called with the appropriate parameters: position, radius, pen width, and color.
radiusis decremented for each iteration, creating smaller arcs that form a concentric pattern. The
huevalue is also adjusted to change the color of each arc, giving the appearance of a rainbow.
Now to run this program you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer or else you can use this python compiler.
When you run the program and it will open a new window and it will start drawing rainbow and below is the finished drawing of rainbow.
As you can see, we successfully drew the rainbow, I hope you were able to run this program successfully.
Here are some more python drawing tutorials for you:
- Draw Pikachu using python with code.
- Draw doraemon using python turtle.
- Draw shinchan using python turtle.
- Draw I love you using python turtle.
- Draw Batman logo using python turtle.
- Draw Google Logo using python turtle.
- Make a python calculator using turtle.
- Draw christmas tree using python.
- Draw spiderman in python programming.
- Draw Python Logo Using Python.
- Draw Iron Man using python turtle with code.
- Draw A Heart Using python turtle with code.
- Draw car in python turtle with code.
- Draw a Cat In Python Turtle With Code.
- Draw Panda In Python Turtle With Code.
- Draw Smiley Face In Python With Code.
- Draw Snowman In Python Turtle With Code.
I hope you found what you were looking for from this tutorial, and if you want more python guides and tutorials like this, do join our Telegram channel for future updates.
Thanks for reading, have a nice day 🙂