Create Fractal Art with Python Turtle

While I was working on a Python project, I needed to create some fascinating fractal designs. I discovered that Python’s Turtle module is perfect for this task! Fractals are complex patterns that repeat at different scales, creating mesmerizing visual effects.

In this article, I will show you how to create beautiful fractals using Python Turtle. Whether you’re a beginner or an experienced programmer, you’ll be able to generate impressive fractal art with just a few lines of code.

So let’s get started!

What are Fractals?

Fractals are infinitely complex patterns that are self-similar across different scales. They’re created by repeating a simple process over and over in an ongoing feedback loop.

Nature is full of fractals, from snowflakes and lightning bolts to the coast of California and the branches of an oak tree. They’re all around us!

Set Up Your Python Environment

Before we start coding fractals, let’s make sure you have everything you need:

# The Turtle module comes with standard Python installation
import turtle

The Turtle module is included in the standard Python library, so you don’t need to install anything extra if you already have Python installed.

Method 1: Create a Simple Koch Snowflake

The Koch Snowflake is one of the most famous fractals and a perfect starting point for our fractal journey.

import turtle

def koch_curve(t, order, size):
    if order == 0:
        t.forward(size)
    else:
        for angle in [60, -120, 60, 0]:
            koch_curve(t, order-1, size/3)
            t.left(angle)

# Set up the turtle
screen = turtle.Screen()
screen.bgcolor("black")
t = turtle.Turtle()
t.speed(0)  # Fastest speed
t.penup()
t.goto(-150, 90)  # Position for the snowflake
t.pendown()
t.color("cyan")  # Light blue color reminiscent of snow

# Draw the Koch Snowflake
for i in range(3):
    koch_curve(t, 4, 300)
    t.right(120)

turtle.done()

You can refer to the screenshot below to see the output.

fractal python

The Koch Snowflake is created by repeatedly replacing each line segment with four segments that form an equilateral bump. The result resembles the intricate patterns of a snowflake, perfect for representing a winter scene in North America!

Read Python Turtle Draw Letters

Method 2: Draw a Sierpinski Triangle

The Sierpinski Triangle is another classic fractal that’s easy to create with Python Turtle.

import turtle

def sierpinski(t, points, degree):
    colormap = ['blue', 'red', 'green', 'white', 'yellow', 'orange']
    t.fillcolor(colormap[degree % 6])
    t.begin_fill()

    t.goto(points[0][0], points[0][1])
    t.goto(points[1][0], points[1][1])
    t.goto(points[2][0], points[2][1])
    t.goto(points[0][0], points[0][1])

    t.end_fill()

    if degree > 0:
        # Calculate midpoints
        p1 = [(points[0][0] + points[1][0]) / 2, (points[0][1] + points[1][1]) / 2]
        p2 = [(points[1][0] + points[2][0]) / 2, (points[1][1] + points[2][1]) / 2]
        p3 = [(points[0][0] + points[2][0]) / 2, (points[0][1] + points[2][1]) / 2]

        # Recursively draw smaller triangles
        sierpinski(t, [points[0], p1, p3], degree-1)
        sierpinski(t, [p1, points[1], p2], degree-1)
        sierpinski(t, [p3, p2, points[2]], degree-1)

# Set up the screen and turtle
screen = turtle.Screen()
screen.bgcolor("black")
t = turtle.Turtle()
t.speed(0)
t.hideturtle()
t.penup()

# Define the points of the initial triangle
# Creating a shape reminiscent of the Rocky Mountains
points = [[-200, -150], [200, -150], [0, 173]]

# Draw the Sierpinski Triangle
sierpinski(t, points, 5)

turtle.done()

You can refer to the screenshot below to see the output.

python fractal

The Sierpinski Triangle is created by repeatedly removing the center triangle from a larger triangle. The result resembles the mountain ranges found across the American landscape!

Check out Python Turtle Input

Method 3: Create a Pythagoras Tree

The Pythagoras Tree is a plane fractal constructed from squares, creating a pattern that resembles a tree.

import turtle
import math

def pythagoras_tree(t, size, level):
    if level == 0:
        return

    # Draw the main square (trunk)
    t.fillcolor("brown" if level < 3 else "green")
    t.begin_fill()
    for i in range(4):
        t.forward(size)
        t.left(90)
    t.end_fill()

    # Save the current position and orientation
    x, y = t.position()
    angle = t.heading()

    # Move to the position for the right branch
    t.penup()
    t.forward(size)
    t.left(90)
    t.forward(size)
    t.right(45)
    t.pendown()

    # Draw the right branch
    pythagoras_tree(t, size * 0.7, level - 1)

    # Return to the original position and orientation
    t.penup()
    t.goto(x, y)
    t.setheading(angle)
    t.pendown()

    # Move to the position for the left branch
    t.penup()
    t.forward(size)
    t.left(90)
    t.forward(size)
    t.left(45)
    t.pendown()

    # Draw the left branch
    pythagoras_tree(t, size * 0.7, level - 1)

    # Return to the original position and orientation
    t.penup()
    t.goto(x, y)
    t.setheading(angle)
    t.pendown()

# Set up the screen and turtle
screen = turtle.Screen()
screen.bgcolor("sky blue")
t = turtle.Turtle()
t.speed(0)
t.hideturtle()

# Position the turtle for the tree
t.penup()
t.goto(-50, -200)
t.pendown()

# Draw the Pythagoras Tree
pythagoras_tree(t, 80, 7)

turtle.done()

You can refer to the screenshot below to see the output.

fractal python code

This fractal creates a tree-like structure perfect for representing the famous oak trees of the southern United States or the redwoods of California!

Read Python Turtle Pen

Method 4: The Dragon Curve

The Dragon Curve is a fascinating fractal that folds on itself like a piece of paper.

import turtle

def dragon_curve(t, order, size, direction=45):
    if order == 0:
        t.forward(size)
    else:
        t.right(direction)
        dragon_curve(t, order-1, size, 45)
        t.left(direction * 2)
        dragon_curve(t, order-1, size, -45)
        t.right(direction)

# Set up the screen and turtle
screen = turtle.Screen()
screen.bgcolor("black")
t = turtle.Turtle()
t.speed(0)
t.pencolor("red")
t.pensize(2)

# Position the turtle
t.penup()
t.goto(-100, 0)
t.pendown()

# Draw the Dragon Curve
dragon_curve(t, 10, 5)

turtle.done()

The resulting pattern resembles the winding nature of the Mississippi River as it snakes through the American heartland!

Check out Python Turtle Grid

Method 5: Mandelbrot Set Visualization

The Mandelbrot Set is one of the most famous fractals. While a full rendering requires more complex code, we can create a simplified version with Turtle.

import turtle
import math

def mandelbrot(h, w, max_iter):
    # Create a new turtle
    t = turtle.Turtle()
    t.speed(0)
    t.penup()

    # Create screen
    screen = turtle.Screen()
    screen.setup(width=w, height=h)
    screen.bgcolor("black")

    # Set the world coordinates
    screen.setworldcoordinates(-2, -1.5, 1, 1.5)

    # Hide the turtle
    t.hideturtle()

    # Draw the Mandelbrot set
    for x in range(-200, 100, 4):
        for y in range(-150, 150, 4):
            zx, zy = x / 100, y / 100
            c = zx + zy * 1j
            z = c

            for i in range(max_iter):
                if abs(z) > 2.0:
                    # Color based on how quickly the point escapes
                    t.color((i % 4 * 64, i % 8 * 32, i % 16 * 16))
                    t.goto(zx, zy)
                    t.dot(2)
                    break
                z = z * z + c

# Draw a simplified Mandelbrot set
mandelbrot(600, 800, 30)

turtle.done()

The Mandelbrot Set, with its infinite complexity, represents the vastness and diversity of the American landscape, from the Grand Canyon to the Florida Keys!

Tips for Creating Better Fractals

  1. Adjust the recursion depth: More recursion creates more detailed fractals but takes longer to render.
  2. Experiment with colors: Try different color schemes to enhance the visual appeal of your fractals.
  3. Use turtle.tracer(0, 0): This disables animation during drawing, making complex fractals render much faster.
  4. Try different starting shapes: Most fractals can be applied to different base shapes for varied results.
  5. Save your creations: Add the following code to save your fractal as an image:
   screen.getcanvas().postscript(file="my_fractal.eps")

Fractals are not just beautiful to look at – they have practical applications in computer graphics, telecommunications, and even in modeling natural phenomena like weather patterns across the United States.

I hope you found this tutorial helpful! Fractal art with Python Turtle is a fascinating blend of mathematics, programming, and creativity. Start with these examples and then let your imagination run wild with your fractal creations!

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.