In this lesson i want to show you How to Draw BarChart with Python Turtle.
What is Python Turtle ?
Python Turtle
is module in the Python programming language that allows you to create simple graphics using virtual turtle that you can control with code. the turtle moves across the screen, drawing lines as it goes and you can use functions in the turtle
module to control its position, shape, color and other properties.
Python Turtle
is fun and educational tool for learning basic graphics programming, as well as for prototyping simple animations and games. with Python Turtle
you can easily create shapes, patterns and drawings using few lines of code, making it an ideal tool for beginners learning to code or for children learning to program.
turtle
module is included in the standard library of Python, so you can start using it right away, without the need to install any additional packages. turtle
module provides simple and intuitive interface and its commands are easy to understand and it makes it best choice for anyone looking to learn the basics of graphics programming.
This is an example of how you can use the turtle
module to draw a bar chart for a given set of data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import turtle def draw_bar(t, height): """ Draw a bar for the given height. """ t.begin_fill() t.left(90) t.forward(height) t.write(" "+ str(height)) t.right(90) t.forward(40) t.right(90) t.forward(height) t.left(90) t.end_fill() data = [48, 117, 200, 240, 160, 260, 220] max_height = max(data) # initialize the turtle turtle.setup(width=1000, height=800, startx=0, starty=0) t = turtle.Turtle() t.color("blue", "red") t.pensize(3) # set up the x and y axis t.penup() t.goto(-300, 0) t.pendown() t.forward(600) t.right(90) t.forward(max_height) t.left(90) t.forward(600) t.left(180) # draw the bars for i, height in enumerate(data): x = i * 50 draw_bar(t, height) turtle.done() |
This code creates bar chart with seven bars each representing a value in data
list. draw_bar
function takes turtle
object and the height of the bar and it uses the turtle to draw bar with the given height. turtle
is first set up with specific width and height and the x and y axis are drawn using the forward
and right
methods. finally bars are drawn using the draw_bar
function and the turtle screen is closed using the turtle.done()
method.
Note that turtle
module is not ideal for drawing complex or highly customizable charts but it can be fun way to learn basic graphics programming in Python.
Run the complete code and this will be the result.
Learn More on Python
- PyQt6: The Ultimate GUI Toolkit for Python
- Python: The Most Versatile Programming Language of the 21st Century
- Tkinter: A Beginner’s Guide to Building GUI Applications in Python
- PySide6: The Cross-Platform GUI Framework for Python
- The Ultimate Guide to Kivy: Building Cross-Platform Apps with Python
- Discover the Power of Django: The Best Web Framework for Your Next Project
- How to Earn Money with Python
- Why Flask is the Ideal Micro-Web Framework
- Python Pillow: The Ultimate Guide to Image Processing with Python
- Get Started with Pygame: A Beginner’s Guide to Game Development with Python
- Python PyOpenGL: A Guide to High-Performance 3D Graphics in Python
- The Cross-Platform Game Development Library in Python
- Unleash the Power of Computer Vision with Python OpenCV
- Unleash the Power of Automated Testing with Python Selenium