# HSSlive: Plus One & Plus Two Notes & Solutions for Kerala State Board

## How to Draw a Decision Tree Using Python Turtle, Tkinter, Matplotlib & Without Libraries

How to Draw a Decision Tree in Python - There are various methods to create a Decision Tree using python libraries like turtle, tkinter and matplotlib. Also, you will learn how to make a Decision Tree using python in easy and simple steps. We have also provided the python code for drawing a Decision Tree pattern. If you have introductory to intermediate knowledge in Python and statistics, then you can use this article as a one-stop shop for building and plotting Decision Tree patterns in Python using libraries from its scientific stack, including NumPy, Matplotlib, Pandas, and Seaborn.

Plotting a Decision Tree in python is a simple procedure, and python in its simplest provides multiple easy methods to do so.

## How to Draw a Decision Tree using Python?

Given below is the process to draw a Decision Tree in python using libraries like Turtle, Tkinter, Matplotib and others.

Four Method to make a Decision Tree in Python using multiple libraries:

1. How to draw a Decision Tree using python turtle?
2. How to draw a Decision Tree using python tkinter?
3. How to draw a Decision Tree using python matplotlib?
4. How to draw a Decision Tree using python code without library?

## How to Draw a Decision Tree using Python Turtle?

Check below step by step process to make a Decision Tree using the python turtle library.

Step

Code

# drawTree.py
import turtle as p

def maketree(x,y):
# p = Turtle()
p.color("green")
p.pensize(5)
#p.setundobuffer(None)
p.hideturtle()
#Make the turtle inbisible.It's a good idea to do this while
# you're in the middle of doing some complex drawing,
#because hiding the turtle speeds up the drawing observably.
p.speed(10000)
p.getscreen().tracer(30,0)
#return the TurtleScreen object the turtle is drawing on.
#TurtleScreen methods can then be called for that object.
P.left(90)# Turn turtle left by angle units.direction Adjusting the brush

p.penup() #Pull the pen up - no drawing when moving.
p.goto(x,y) #Move turtle to an absolute position.
#If the pen is down, draw line. Do not change the turtle's orientation.
p.pendown() # Pull the pen down - drawing when moving.
# These three statements are a combination equivalent to first put the pen up and then move to the specified position, then put the pen on the start drawing
# Otherwise, the turtle will automatically draw the line as soon as it moves.

#t = tree([p], 200, 65, 0.6375)
t = tree([p], 110, 65, 0.6375)
Print(len(p.getscreen().turtles())) #How many turtles are drawn

def tree(plist, l, a, f):
"""plist is list of pensize
l is length of branch
a is half of the angle between 2 branches
f is factor by which branch is shortened
from level to level."""
if l > 5:
lst = []
for p in plist:
P.forward(l)# draws in the current direction
#Move the turtle forward by the specified distance,
# in the direction the turtle is headed.
q = p.clone() #Create and return a clone of the turtle
#with same position, heading and turtle properties.
p.left(a) #Turn turtle left by angle units
q.right(a) # turn turtle right by angle units.nits are
#by default degrees, but can be set via the degrees() and
Lst.append(p) #Add elements to the end of the list
lst.append(q)
tree(lst, l*f, a, f)

def main():
maketree(-200,-200)
# maketree(0,0)
# maketree( 200,-200)
p.done()

main()

Output

 How to Draw a Decision Tree Using Python Turtle, Tkinter, Matplotlib & Without Libraries

## How to Draw a Decision Tree using Python Tkinter?

Check below step by step process to make a Decision Tree using the python tkinter library.

Step

Code

Output

## How to Draw a Decision Tree using Python Matplotlib?

Check below step by step process to make a Decision Tree using the python matplotlib library.

Step

Code

Output

## How to Draw a Decision Tree using Python Code without Library?

Step

Code

Output

Note: Also, these codes might not run in an online compiler please use an offline compiler. Programmers can make any changes in the code according to their specific requirement.

## Conclusion on How to Draw a Decision Tree using Python

We hope the programs and methods provided above on how to make a Decision Tree using python libraries have been helpful to you. If there is any issue in any of the code please let us know in the comments. We will try to resolve it as soon as possible.

Share: