import matplotlib.pyplot as plt
import numpy as np
def draw_branch(ax, x, y, length, angle, depth, branch_factor=0.7, angle_variation=30):
if depth == 0:
return
new_x = x + length * np.cos(np.radians(angle))
new_y = y + length * np.sin(np.radians(angle))
ax.plot([x, new_x], [y, new_y], 'brown', lw=depth)
draw_branch(ax, new_x, new_y, length * branch_factor, angle + angle_variation, depth - 1)
draw_branch(ax, new_x, new_y, length * branch_factor, angle - angle_variation, depth - 1)
def fractal_tree():
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_xticks([])
ax.set_yticks([])
ax.set_frame_on(False)
draw_branch(ax, 0, -1, 1, 90, depth=10)
plt.xlim(-1, 1)
plt.ylim(-1, 1.5)
plt.title('Fractal tree pattern plot')
plt.show()
fractal_tree()
#source code --> clcoding.com
Code Explanation:
1. Importing Required Libraries
import matplotlib.pyplot as plt
import numpy as np
matplotlib.pyplot: Used to plot the fractal tree.
numpy: Used for mathematical operations like cos and sin (for angle calculations).
2. Recursive Function to Draw Branches
def draw_branch(ax, x, y, length, angle, depth, branch_factor=0.7, angle_variation=30):
This function draws branches recursively.
Parameters:
ax: The matplotlib axis for plotting.
(x, y): The starting coordinates of the branch.
length: The length of the current branch.
angle: The angle at which the branch grows.
depth: The recursion depth, representing how many branch levels to draw.
branch_factor: Determines how much shorter each successive branch is.
angle_variation: Determines how much the new branches deviate from the main branch.
3. Base Condition (Stopping Recursion)
if depth == 0:
return
When depth == 0, recursion stops, meaning the smallest branches are reached.
4. Calculating the New Branch Endpoints
new_x = x + length * np.cos(np.radians(angle))
new_y = y + length * np.sin(np.radians(angle))
Uses trigonometry to compute the (x, y) coordinates of the new branch:
cos(angle): Determines the horizontal displacement.
sin(angle): Determines the vertical displacement.
np.radians(angle): Converts degrees to radians.
5. Drawing the Branch
ax.plot([x, new_x], [y, new_y], 'brown', lw=depth)
The branch is drawn as a brown line.
lw=depth: Line width decreases as depth decreases (thicker at the bottom, thinner at the tips).
6. Recursively Creating Two New Branches
draw_branch(ax, new_x, new_y, length * branch_factor, angle + angle_variation, depth - 1)
draw_branch(ax, new_x, new_y, length * branch_factor, angle - angle_variation, depth - 1)
Two smaller branches sprout from the current branch at:
angle + angle_variation (left branch).
angle - angle_variation (right branch).
Each new branch has:
A reduced length (length * branch_factor).
One less depth (depth - 1).
7. Creating the Fractal Tree
def fractal_tree():
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_xticks([])
ax.set_yticks([])
ax.set_frame_on(False)
Creates a figure and axis for plotting.
Removes ticks (set_xticks([]), set_yticks([])) and frame (set_frame_on(False)) for a clean look.
8. Calling the Recursive Function
draw_branch(ax, 0, -1, 1, 90, depth=10)
Starts drawing the tree at (0, -1), which is near the bottom center.
The initial branch:
Length = 1
Angle = 90° (straight up)
Depth = 10 (controls the number of branch levels).
9. Setting Plot Limits & Displaying the Tree
plt.xlim(-1, 1)
plt.ylim(-1, 1.5)
plt.title('Fractal tree pattern plot')
plt.show()
Limits the x-axis (-1 to 1) and y-axis (-1 to 1.5) to keep the tree centered.
Displays the fractal tree with plt.show().
0 Comments:
Post a Comment