Create a Rainbow in Python Using Various Methods

Rainbow in Python is not a function. It is simply creating a rainbow of colors for different purposes. We can use various methods to create a rainbow in Python. It all depends on your requirements and needs, you can use any of the following methods to create a rainbow in Python. In this short article, we will be using turtle and matplotlib modules to create a rainbow in Python.

You may also like to visualize your data in 3d in Python.

Create a Rainbow in Python

As you know the rainbow is a combination of seven different colors where the first color is red and the last color is violet.


In this short article, we will create a rainbow using the following different methods:

  • Rainbow in Python using the turtle module
  • Rainbow Python using matplotlib module

Let us start to build a rainbow in Python using these two methods.

Rainbow Using Turtle in Python

Turtle is a built-in module in Python that provides a drawing screen and pen(turtle) to draw various shapes. Similar to physical drawing, in the Turtle module we have to move the pen to draw different shapes. Various functions are available that help moves the pen in different directions.

Now let us create a rainbow in Python using the turtle module and then we will explain the code

# Import turtle package
import turtle
# Creating a turtle screen object
sc = turtle.Screen()
# Creating a turtle object(pen)
pen = turtle.Turtle()
# Defining a method to form a semicircle
def semi_circle(col, rad, val):
    # Set the fill color of the semicircle
    # Draw a circle, -180)
    # Move the turtle to air
    # Move the turtle to a given position
    pen.setpos(val, 0)
    # Move the turtle to the ground
# Set the colors for drawing
col = ['violet', 'indigo', 'blue',
       'green', 'yellow', 'orange', 'red']
# Setup the screen features
sc.setup(600, 600)
# Set the screen color to black
# Setup the turtle features
# Loop to draw 7 semicircles
for i in range(7):
    semi_circle(col[i], 10*(
      i + 8), -10*(i + 1))
# Hide the turtle



In the example above, the semi_circle function takes three parameters including color, radius, and value. This function uses the turtle (pen) to set the color of the semi-circle with a given radius.

First, the turtle set up the screen including the speed and width. We have defined a loop for 7 colors so that each loop will create a different color:

Creating Rainbow in Python Using Matplotlib

Matplotlib module has many valuable functions to create different types of plots. Although there is no function to create a rainbow we can use the matplotlib module to create a rainbow shape. Let us first create a rainbow in Python using matplotlib module and then we will explain the code.

# importing the module
import matplotlib.pyplot as plt

# creating subplots
fig, ax = plt.subplots()

# fiing the axis of the rainbow
ax.set(xlim=(10, 0), ylim=(10, -10))

# using for loop to create rainbow color
for i in range(0, 7):
   rainbow =  '#FF0000', '#FF7F00','#FFFF00',  '#00FF00', '#0000FF',  '#4B0082',  '#9400D3'
   c = plt.Circle((0, 0), 7-i, fill=True, color=rainbow[i])



As you can see we have created a rainbow in Python using matplotlib module. First, we imported the module and then defined the axis of the plot. Then we used for loop to create a rainbow with 7 different colors:

We can also use rainbow colors as indicators in the matplotlib module. For example, we may want different scattered plots to have a rainbow color indicator. Here is the code for it:

# importing the modules
import matplotlib.pyplot as plt
import numpy as np

# Create an array of evenly spaced values between 0 and 1
x = np.linspace(0, 1, 256)

# Create a rainbow colormap with 256 colors
cmap =

# Plot the rainbow colormap
plt.imshow(x.reshape(1, 256), cmap=cmap, interpolation='nearest')



As you can see, there is a rainbow indicator.


In this article, we learned how we can create a rainbow in Python using various modules. We create a rainbow using turtle and matplotlib modules.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top