Fundamentals 5 min read

How to Create a Fractal Tree with Python’s Matplotlib and Recursion

This guide demonstrates how to generate a fractal tree in Python using Matplotlib, covering library imports, functions for rotation, distance calculation, split-point determination, and a recursive drawing algorithm that visualizes branching structures.

Model Perspective
Model Perspective
Model Perspective
How to Create a Fractal Tree with Python’s Matplotlib and Recursion

We now use Python’s Matplotlib library to draw a fractal tree, employing iteration and recursion.

Import required libraries

import matplotlib.pyplot as plt
import math
import random

Define rotation angle

def rotate(x, y, angle):
    x_point = (x[0] + (math.cos(math.radians(angle))) * (x[1] - x[0])
               - math.sin(math.radians(angle)) * (y[1] - y[0]))
    y_point = (y[0] + math.sin(math.radians(angle)) * (x[1] - x[0])
               + math.cos(math.radians(angle)) * (y[1] - y[0]))
    return x_point, y_point

Calculate distance between two points

def distance(x, y):
    return math.sqrt((x[1] - x[0])**2 + (y[1] - y[0])**2)

Find split point

def find(x, y, d1, d2):
    ratio = d1 / d2
    n_x = (1 - ratio) * x[0] + ratio * x[1]
    n_y = (1 - ratio) * y[0] + ratio * y[1]
    return n_x, n_y

Draw recursive tree

def tree(x, y, count, angle, n):
    if count > 0:
        count -= 1
        dis = distance(x, y)
        plt.plot(x, y, linewidth=count + 1)
        d1 = dis + (dis / 2)
        n_x, n_y = find(x, y, d1, dis)
        x1, y1 = rotate([x[1], n_x], [y[1], n_y], angle)
        plt.plot([x[1], x1], [y[1], y1], linewidth=count)
        x2, y2 = rotate([x[1], n_x], [y[1], n_y], 0 - angle)
        plt.plot([x[1], x2], [y[1], y2], linewidth=count)
        tree([x[1], x1], [y[1], y1], count, angle, n)
        tree([x[1], x2], [y[1], y2], count, angle, n)
        plt.plot([x[1], n_x], [y[1], n_y], linewidth=count)
        tree([x[1], n_x], [y[1], n_y], count, angle, n)

x = [2, 2]
y = [0, 6]
angle = 45
tree(x, y, 6, angle, 3)
plt.show()

The resulting image shows a branching fractal tree.

Fractal tree drawn with Matplotlib
Fractal tree drawn with Matplotlib
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonRecursionfractalData visualization
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.