In this Python tutorial, we will learn How to create a Grid in Python Turtle and we will also cover different examples related to Turtle Grid. And, we will cover these topics.
- Python turtle grid
- Python turtle coordinate grid
Python Turtle Grid
In this section, we will learn about how to create a grid in python turtle.
Before moving forward, we should have a piece of knowledge about what is a grid?
A grid is defined as a network of horizontal or vertical lines. These lines create a connection with each other or we can say that hold each other. Grid is used for giving the location to points on the chart.
Turtle grid is also used for mentioning the point on the correct in the graph. And these points are mentioned with the help of the turtle.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() module is used to make objects.
- tur.forward(300) is used to move the turtle in the forward direction.
- tur.up() is used to stop the drawing.
- tur.setpos(val,300) is used to set the position of the y axis.
- tur.down() is used to start the drawing.
- tur.backward(300) is used to move the turtle in the backward direction for drawing another line.
- tur.write(0,font=(“Verdana”, 18, “bold”)) is used to write the 0 in the grid.
- ws.setup(800,800) is used to set the screen.
- tur.speed(100) is used to give the speed to the turtle.
- tur.left(90) is used to move the turtle in the left direction.
- tur.hideturtle() is used the turtle is not shown on the screen.
from turtle import *
import turtle
ws=turtle.Screen()
tur=turtle.Turtle()
def draw_yaxs(val):
tur.forward(300)
tur.up()
tur.setpos(val,300)
tur.down()
tur.backward(300)
tur.up()
tur.setpos(val+10,0)
tur.down()
def draw_xaxs(val):
tur.forward(300)
tur.up()
tur.setpos(300,val)
tur.down()
tur.backward(300)
tur.up()
tur.setpos(0,val+10)
tur.down()
def label():
tur.penup()
tur.setpos(155,155)
tur.pendown()
tur.write(0,font=("Verdana", 18, "bold"))
tur.penup()
tur.setpos(290,155)
tur.pendown()
tur.write("x",font=("Verdana", 18, "bold"))
tur.penup()
tur.setpos(155,290)
tur.pendown()
tur.write("y",font=("Verdana", 18, "bold"))
ws.setup(800,800)
tur.speed(100)
tur.left(90)
tur.color('blue')
for i in range(30):
draw_yaxs(10*(i+1))
tur.right(90)
tur.up()
tur.setpos(0,0)
tur.down()
for i in range(30):
draw_xaxs(10*(i+1))
tur.hideturtle()
turtle.done()
Output:
After running the above code, we get the following output in which we can see the horizontal and vertical lines which connected each other and formed a beautiful colored grid.
Also, check: Fractal Python Turtle
Python Turtle coordinate grid
In this section, we will learn about how to create a coordinate grid in python turtle.
As we know the grid is a network of horizontal and vertical lines. The horizontal and vertical lines create a connection with each other and form a beautiful grid. Inside the grid, we create a coordinate on which point is placed at its proper location.
A coordinate plane has an x-axis and y-axis which is used to detect points or we can say that determines the location of points.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.forward(300) is used to move the lines in the forward direction with the help of a turtle.
- tur.up() is used to stop the drawing.
- tur.setpos(val,300) is used to set the position of axis X or Y.
- tur.down() is used to start the drawing.
- tur.backward(300) is used to move the lines in the backward direction with the help of the turtle.
- tur.write(0,font=(“Verdana”, 18, “bold”)) is used to write the 0 at the origin of the coordinates.
- tur.write(“x”,font=(“Verdana”, 18, “bold”)) is used to write the x where the x axis is placed.
- tur.write(“y”,font=(“Verdana”, 18, “bold”)) is used to write the y where the Y axis is placed.
- ws.setup(800,800) is used to set the a size of screen.
- tur.speed(100) is used to give the speed to turtle for draw some shapes.
- tur.color(‘cyan’) is used to give color to the pen.
- label() is used to give the labels to coordinates.
- tur.hideturtle() is used for hiding the turtle from the screen.
from turtle import *
import turtle
ws=turtle.Screen()
tur=turtle.Turtle()
def draw_yaxs(val):
tur.forward(300)
tur.up()
tur.setpos(val,300)
tur.down()
tur.backward(300)
tur.up()
tur.setpos(val+10,0)
tur.down()
def draw_xaxs(val):
tur.forward(300)
tur.up()
tur.setpos(300,val)
tur.down()
tur.backward(300)
tur.up()
tur.setpos(0,val+10)
tur.down()
def label():
tur.penup()
tur.setpos(155,155)
tur.pendown()
tur.write(0,font=("Verdana", 18, "bold"))
tur.penup()
tur.setpos(290,155)
tur.pendown()
tur.write("x",font=("Verdana", 18, "bold"))
tur.penup()
tur.setpos(155,290)
tur.pendown()
tur.write("y",font=("Verdana", 18, "bold"))
ws.setup(800,800)
tur.speed(100)
tur.left(90)
tur.color('cyan')
for i in range(30):
draw_yaxs(10*(i+1))
tur.right(90)
tur.up()
tur.setpos(0,0)
tur.down()
for i in range(30):
draw_xaxs(10*(i+1))
tur.color('black')
tur.up()
tur.setpos(0,150)
tur.down()
tur.forward(300)
tur.left(90)
tur.up()
tur.setpos(150,0)
tur.down()
tur.forward(300)
label()
tur.hideturtle()
turtle.done()
Output:
After running the above code, we get the following output in which we can see a grid is created inside the grid coordinates are drawn.
As we know the coordinate plane has two axis x-axis and y-axis which are used to detect the points and we see the x-axis and y-axis which represent the coordinate plane.
Also, take a look at some more tutorials on Python Turtle.
- Python Clear Turtle with examples
- Python Turtle Get Position
- Python Turtle 3d Shapes
- Python Turtle Draw Line
- Python Turtle Star
- Python Turtle Oval
- Python Turtle Nested Loop
- Python turtle draw letters
- Python Turtle Setworldcoordinates
So, in this tutorial, we discussed Python Turtle Grid and we have also covered these examples related to its implementation. Here is the list of examples that we have covered.
- Python turtle grid
- Python turtle coordinate grid
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.