Skip to content

F. Drawing a pie chart with the turtle


🎯 Learning Goals

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

  • ○ Understand what a pie chart is
  • ○ Convert percentages into angles
  • ○ Draw a pie slice with the turtle

Core Concepts

What is a Pie Chart?

Pie graphshows how much of the whole each part takes up, its Proportion.

🍕 Analogy: Sharing a pizza

Imagine sharing a whole pizza with your friends!

  • If I eat half (50%) → a half-circle size
  • If a friend eats 1/4 (25%) → a quarter size
  • The whole pizza = 100% = 360 degrees

Percentage → angle conversion

Proportion Calculation Angle
50% 360 × 0.5 180°
25% 360 × 0.25 90°
10% 360 × 0.1 36°

Formula: angle = 360 × (percentage / 100)


Drawing a sector with the turtle

Steps Action
1 Start at the center
2 Move forward by the radius
3 Draw the arc
4 Return to the center

Try Visual Coding

Let's start by drawing a circle!

🎯 Mission
Try drawing a circle with the turtle
# Add blocks and the Python code will show up here...
Add blocks and the flowchart will show up here...
100 90 90 50 4


Example Code

Drawing a pie slice with a function

Let's turn the code that draws a pie slice into a Functionso we can reuse it!

import turtle

def draw_pie_slice(t, radius, angle, color):
    """A function that draws one pie slice"""
    t.fillcolor(color)
    t.begin_fill()

    t.forward(radius)          # Move forward by the radius
    t.left(90)                 # Left 90 degrees
    t.circle(radius, angle)    # Draw the arc
    t.left(90)                 # Left 90 degrees
    t.forward(radius)          # Return to the center
    t.left(180 - angle)        # Starting position for the next slice

    t.end_fill()

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

# Draw a half-circle (50%) with a function call!
draw_pie_slice(t, 80, 180, "red")

What Happens

A red half-circle (50%) is drawn! 🔴

💡 Advantages of functions

  • Reusable: Make it once, use it many times
  • Clean: call complex code in a single line
  • Parameter: radius, angle, colorcan be freely changed

A 2-slice pie chart with functions

import turtle

def draw_pie_slice(t, radius, angle, color):
    """A function that draws one pie slice"""
    t.fillcolor(color)
    t.begin_fill()

    t.forward(radius)
    t.left(90)
    t.circle(radius, angle)
    t.left(90)
    t.forward(radius)
    t.left(180 - angle)

    t.end_fill()

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

radius = 80

# Data: Like 70%, Dislike 30%
data = [70, 30]
colors = ["green", "red"]

# Use functions to keep it clean!
for i in range(2):
    angle = 360 * data[i] / 100  # Percentage → angle
    draw_pie_slice(t, radius, angle, colors[i])

What Happens

A pie chart split into green (70%) and red (30%)! Just 2 lines inside the loopmake it very clean.


🎯 Quiz

Quiz: Pie chart
How many degrees does 25% take up in a pie chart?
Show Answer

Answer: B) 90 degrees

Calculation: 360 × 0.25 = 90 degrees

25% is 1/4 of the whole, so it's 90 degrees, a quarter of the circle!

Pie chart angle calculation:

  • Angle for 50%: degrees
  • Angle for 10%: degrees
  • Full circle: degrees

🎯 Fill-in-the-Blank Mission

Fill in the blanks to complete the code!

Check answers

for percent in data:
    angle = 360 * percent / 100
    print(f'{percent}% = {angle}degrees')
Result: 60% = 216 degrees, 40% = 144 degrees

Check answers

t.circle(radius, angle)
- circle(radius, angle)draws an arc


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

The pie chart in the following code looks wrong. What's the problem?

import turtle

t = turtle.Turtle()

# Data: Like, Neutral, Dislike
data = [50, 30, 10]  # Sum is 90%?!
colors = ["green", "yellow", "red"]

for i in range(3):
    angle = 360 * data[i] / 100
    # Draw the pie slices...
Show Hint

In a pie chart, what should all the percentages add up to?

Show Answer

The percentages don't add up to 100%!

50 + 30 + 10 = 90% (10% short!)

# Correct data (sums to 100%)
data = [50, 30, 20]  # 50 + 30 + 20 = 100%

# Or
data = [50, 30, 10, 10]  # Add Other 10%

In a pie chart, the percentages of all slices must add up to 100%! Otherwise the circle won't be completely filled, or it will overflow.


🔧 Debug Clinic

Problem: the slices overlap!

❌ Wrong Code

t.forward(radius)
t.circle(radius, angle)
# Doesn't set the starting position for the next slice!

✅ Correct Code

t.forward(radius)
t.left(90)
t.circle(radius, angle)
t.left(90)
t.forward(radius)
t.left(180 - angle)  # Starting position for the next slice!

Try Coding It Yourself

Making a 2-slice pie chart with functions

draw_pie_slice() Complete the function to draw a 2-slice pie chart!


🧩 Key Takeaways

What you learned in this lesson:

Concept Description
Pie graph Represent percentages as circle slices
Angle calculation 360 × (percentage/100)
circle(r, angle) Draw an arc with radius r and angle angle
draw_pie_slice() function Make pie slice drawing reusable
Using lists data[i], colors[i]to apply the data in order

Review Checklist

  • [ ] Can you convert percentages into angles?
  • [ ] draw_pie_slice() Can you make a function?
  • [ ] Can you use loops and functions together to draw multiple slices?