Plotting a Sine Wave¶
By the end of this lesson you'll be able to:
- Import the
mathlibrary and usemath.sin()to calculate sine values - Use a
forloop withgoto()to plot a mathematical function across the canvas - Adjust the amplitude and color of the wave by changing two variables
Turtle graphics aren't just for shapes — you can use them to plot math functions. The turtle visits hundreds of points along the x-axis, calculates the y-value for each, and connects them with goto(). The result looks like a graph, drawn live.
Welcome to This Lesson!
Did you know Python can draw math? Today you'll plot a sine wave — the shape of ocean waves,
sound, and light — right in the browser. Let's code it together!
The Sine Function¶
The sine function math.sin(angle) takes an angle in radians and returns a value between -1 and 1. A full sine wave goes from 0 up to 1, back down through 0 to -1, and back up to 0 — a smooth S-shaped curve.
Two steps to plot it with turtle:
1. Loop x from -180 to 180
2. Convert x to radians with math.radians(x), then call math.sin() to get y
3. Multiply y by an amplitude (like 80) to scale it up to canvas size
4. Use goto(x, y * amplitude) to move the turtle
1 2 3 4 | |
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
What Do You Think Will Happen?
The turtle will visit 360 points from x = -180 to x = 179 and draw a line through all of them.
What shape do you expect to see? Will it look smooth or jagged?
Make your guess — then click Run!
Try It Now¶
Edit the code below and click Run to see the result right on this page. No account needed — everything runs in your browser.
Were you right? A smooth S-curve stretching across the canvas — one full cycle of the sine function, scaled to 80 pixels tall.
How It Works¶
math.radians(x) converts degrees to radians (the unit math.sin() expects). The returned value is between -1 and 1, so multiplying by amplitude (80) stretches it to ±80 pixels — a visible wave.
Starting with penup() and goto(-180, 0) positions the turtle at the left edge of the wave without drawing a line there first. Then pendown() starts the wave at exactly x = -180.
Explanation Table¶
| Line | What it does |
|---|---|
import math |
Loads the math library (provides sin, cos, radians, etc.) |
amplitude = 80 |
Controls how tall the wave is — larger = taller |
math.radians(x) |
Converts degrees to radians (required by math.sin) |
math.sin(...) |
Returns a value between -1 and 1 for that angle |
y * amplitude |
Scales the value to canvas coordinates |
monty.goto(x, y * amplitude) |
Moves the turtle to the next point on the curve |
Try math.cos() Too
Replace math.sin with math.cos and see how the wave shifts.
The cosine wave is identical in shape to sine but shifted left by 90 degrees —
so instead of starting at 0 it starts at its peak!
Learning Check¶
Your Turn — Draw Two Waves
The program below draws one sine wave. Add four lines after the first loop ends to:
lift the pen (penup), go back to (-180, 0), lower the pen (pendown),
and draw a second loop in 'red' using math.cos instead of math.sin.
Experiments¶
Try these changes to the sine wave code. Predict first, then run!
-
Change
amplitude = 80toamplitude = 160. You'll know it worked when the wave is twice as tall and the peaks nearly reach the top of the canvas. -
Change
amplitude = 80toamplitude = 30. You'll know it worked when the wave is much flatter — almost a straight line. -
Replace
math.sinwithmath.cos. You'll know it worked when the wave starts at its peak (y = amplitude) instead of at zero. -
Change
range(-180, 180)torange(-360, 360, 2)(step by 2). You'll know it worked when you see two full cycles of the wave across the canvas. -
Multiply the radians by
2before passing tomath.sin:math.sin(math.radians(x) * 2). You'll know it worked when the wave completes two full cycles in the same x range.
Great Work!
You plotted a mathematical function with turtle — you're writing the kind of code scientists
and engineers use to visualize data! Try combining sin and cos in the same loop for interesting patterns.
Let's code it together!