Python program to draw a bar chart using turtle

Graphical representation of data provides an enhanced understanding of complex sub-structures and helps us easily interpret hidden patterns and trends. Python offers a built-in module called turtle that allows us to create visual graphics programmatically.

The turtle module is a built-in Python library that enables drawing graphics on a turtle graphics screen. In this article, we will create a bar chart using the turtle module ?

Understanding the Turtle Module

The turtle module uses a virtual turtle object to create graphics. This turtle can move around the screen and draw shapes. Let's explore the key functions needed to create a bar chart ?

Essential Turtle Functions

  • Turtle() Creates a new turtle object

  • fillcolor() Sets the fill color for drawing shapes

  • begin_fill() Starts the filling process

  • left(angle) Turns the turtle left by specified degrees

  • right(angle) Turns the turtle right by specified degrees

  • forward(distance) Moves the turtle forward by specified units

  • write(text) Writes text at the current turtle position

  • end_fill() Completes the shape filling process

Setting Up the Coordinate System

The turtle module is inspired by LOGO programming language. By default, the turtle starts at the center (0,0). For a bar chart, we need to position it at the lower-left corner using the setworldcoordinates() method.

This method takes four parameters ?

  • Lower-left X and Y coordinates

  • Upper-right X and Y coordinates

Creating a Bar Chart

Here's a complete program that creates a colorful bar chart with different heights ?

import turtle

def draw_bar(turtle_obj, bar_height, bar_color):
    """Draw a single bar with specified height and color"""
    turtle_obj.fillcolor(bar_color)
    turtle_obj.begin_fill()
    
    # Draw the bar rectangle
    turtle_obj.left(90)
    turtle_obj.forward(bar_height)
    turtle_obj.write(str(bar_height))  # Label the bar height
    turtle_obj.right(90)
    turtle_obj.forward(80)  # Bar width
    turtle_obj.right(90)
    turtle_obj.forward(bar_height)
    turtle_obj.left(90)
    
    turtle_obj.end_fill()

# Data for the bar chart
bar_heights = [23, 94, 42, 150, 200, 56, 240, 40]
bar_colors = ["orange", "purple", "green", "red", "black", "grey", "yellow", "violet"]

# Calculate dimensions
max_bar_value = max(bar_heights)
space = 20

# Set up the screen
screen = turtle.Screen()
screen.setworldcoordinates(0 - space, 0 - space, 
                          50 * space, max_bar_value + space)
screen.bgcolor("lightblue")
screen.title("Bar Chart using Turtle")

# Create turtle object
chart_turtle = turtle.Turtle()
chart_turtle.pensize(3)
chart_turtle.speed(5)

# Draw all bars
for i in range(len(bar_heights)):
    draw_bar(chart_turtle, bar_heights[i], bar_colors[i])

# Keep the window open until clicked
screen.exitonclick()

Output

The program creates a colorful bar chart where each bar represents a different value with distinct colors. The bars are drawn sequentially from left to right with their height values labeled on top.

Key Features of the Program

  • Dynamic scaling The coordinate system adjusts based on the maximum bar height

  • Color customization Each bar can have a different color

  • Height labeling Each bar displays its value at the top

  • Interactive display The window remains open until clicked

Enhancements and Limitations

The turtle module is excellent for educational purposes and simple visualizations. You can enhance bar charts by adding:

  • Axis labels and titles

  • Grid lines for better readability

  • Custom bar spacing and widths

For more advanced statistical visualizations and data analysis, consider using libraries like matplotlib, seaborn, or plotly in combination with NumPy and Pandas.

Conclusion

The turtle module provides an intuitive way to create bar charts programmatically. While it's ideal for learning graphics programming and creating simple visualizations, professional data analysis typically requires more specialized plotting libraries for advanced statistical features.

Updated on: 2026-03-27T07:34:41+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements