Skip to content

D. Drawing Bar Graphs with the Turtle


🎯 Learning Goals

By the end of this lesson, you'll be able to:

  • ○ You can draw a bar (rectangle) with the turtle
  • FunctionYou can reuse bar drawing with
  • ○ You can draw several bars side by side

Core Concepts

What Is a Bar Graph?

Bar graphexpresses data as the heightof a bar.

📊 Analogy: Stacking Blocks

Just like stacking Lego blocks, the bigger the number, the more blocks you stack!

  • Number 3 → 3 blocks
  • Number 5 → 5 blocks
  • you can tell which one is taller right away !

Drawing a Single Bar

A bar is a Square. Let's draw a rectangle with the turtle!

# Draw a single bar (height 100)
t.forward(100)    # Up
t.right(90)
t.forward(30)     # Right (bar width)
t.right(90)
t.forward(100)    # Down
t.right(90)
t.forward(30)     # Left

Bar Height = Data Value

Subject Score Bar height
Korean 80 80 pixels
Math 95 95 pixels
English 70 70 pixels

Drawing Bars with a Function

Instead of writing the same code over and over, Functionlet's turn it into one and reuse it!

def draw_bar(t, height, width=40):
    """A function that draws a single bar"""
    for _ in range(2):
        t.forward(height)
        t.right(90)
        t.forward(width)
        t.right(90)

💡 Advantages of functions

  • Reusable: Make it once, use it many times
  • Clean: The code is short and easy to read
  • Easy to modify: Fix just the function and everything changes

Try Visual Coding

Let's draw a single bar with the turtle!

🎯 Mission
Add the turtle start block
Draw a rectangle using forward/right blocks
# Add blocks and the Python code will show up here...
Add blocks and the flowchart will show up here...
100 100 90 90 4


Example Code

Drawing a Single Bar with a Function

import turtle

def draw_bar(t, height, width=40):
    """A function that draws a single bar"""
    for _ in range(2):
        t.forward(height)
        t.right(90)
        t.forward(width)
        t.right(90)

t = turtle.Turtle()
t.shape("turtle")
t.speed(3)

# Move to the starting position
t.penup()
t.goto(-100, -100)
t.pendown()
t.left(90)  # Face up

# Draw the bar with a function call!
draw_bar(t, 80)

What Happens

A single bar with a height of 80 is drawn! Thanks to the function, the code is nice and clean.

Drawing 3 Bars with a Function (Report Card)

import turtle

def draw_bar(t, height, width=40):
    """A function that draws a single bar"""
    for _ in range(2):
        t.forward(height)
        t.right(90)
        t.forward(width)
        t.right(90)

def move_to_next(t, width, gap=20):
    """A function that moves to the next bar position"""
    t.penup()
    t.right(90)
    t.forward(width + gap)
    t.left(90)
    t.pendown()

t = turtle.Turtle()
t.shape("turtle")
t.speed(5)

# Data: Korean, Math, English scores
scores = [80, 95, 70]
width = 40

t.penup()
t.goto(-100, -100)
t.pendown()
t.left(90)  # Face up

# Use functions to keep it clean!
for score in scores:
    draw_bar(t, score, width)
    move_to_next(t, width)

What Happens

The scores for three subjects are shown as a bar graph! Thanks to the function, the inside of the loop is just 2 lines—nice and clean.


🎯 Quiz

Quiz: Bar Graphs
In a bar graph, the bigger the data value, the more the bar does what?
Show Answer

Answer: A) Gets taller

A bar graph expresses data values as the heightof a bar. The bigger the value, the taller the bar; the smaller the value, the shorter the bar!

Bar graph function practice:

Define a function: def (t, height, width):

Call a function: draw_bar(t, , 40)

Advantage of functions: code it


🎯 Fill-in-the-Blank Mission

Fill in the blanks to complete the code!

Check answers
def draw_bar(t, height, width=40):
    for _ in range(2):
        t.forward(height)
        t.right(90)
        t.forward(width)
        t.right(90)

⚠️ Try Breaking It (Let's Break It!)

In the code below, the bar is drawn strangely. What's the problem?

import turtle

def draw_bar(t, height, width=40):
    for _ in range(2):
        t.forward(height)
        t.right(90)
        t.forward(width)
        t.right(90)

t = turtle.Turtle()

# Draw a bar
draw_bar(t, 80)  # The bar is lying on its side!
Show Hint

At first, the turtle Which directionis it facing?

Show Answer

The turtle is facing right, so the bar is drawn sideways!

import turtle

def draw_bar(t, height, width=40):
    for _ in range(2):
        t.forward(height)
        t.right(90)
        t.forward(width)
        t.right(90)

t = turtle.Turtle()
t.left(90)  # ← Face up first!

draw_bar(t, 80)  # Now the bar is drawn going up!

At first, the turtle Rightfaces this way. t.left(90)Use upto make it face up, then draw the bar!


🔧 Debug Clinic

Problem: the bar is lying on its side!

❌ Wrong Code

t.forward(80)  # The turtle is facing right, so it's drawn sideways

✅ Correct Code

t.left(90)      # Face up first!
t.forward(80)   # Now it's drawn going up
Fix: Before drawing the bar, left(90)use this to make it face up!


Try Coding It Yourself

Drawing 2 Bars with a Function

draw_bar() Make a function and draw two subjects' scores as a bar graph!

Example Answer
import turtle

def draw_bar(t, height, width=40):
    """A function that draws a single bar"""
    for _ in range(2):
        t.forward(height)
        t.right(90)
        t.forward(width)
        t.right(90)

t = turtle.Turtle()
t.shape('turtle')
t.speed(3)

scores = [70, 90]
width = 40

t.penup()
t.goto(-80, -100)
t.pendown()
t.left(90)

for score in scores:
    draw_bar(t, score, width)
    t.penup()
    t.right(90)
    t.forward(width + 20)
    t.left(90)
    t.pendown()

Result: Using the function, two bars are drawn nice and clean!


🧩 Key Takeaways

What you learned in this lesson:

Concept Description
Bar graph Expressing data as bar height
draw_bar() function Makes bar drawing reusable
The benefits of functions Code reuse, cleanliness, easy to modify
Multiple bars Efficiently with a loop + function

Review Checklist

  • [ ] draw_bar() Can you make a function?
  • [ ] Can you call a function to draw a bar?
  • [ ] Can you use a loop and a function together?