Barnsley FernΒΆ

Draw a nice fern.

barnsley fern
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

A1 = np.array([[ 0.00,  0.00], [ 0.00, 0.16]])
A2 = np.array([[ 0.85,  0.04], [-0.04, 0.85]])
A3 = np.array([[ 0.20, -0.26], [ 0.23, 0.22]])
A4 = np.array([[-0.15,  0.28], [ 0.26, 0.24]])

b1 = np.array([[0.00], [0.00]])
b2 = np.array([[0.00], [1.60]])
b3 = np.array([[0.00], [1.60]])
b4 = np.array([[0.00], [0.44]])

x = np.array([[0], [0]])

lis = []
for i in range(100_000):
    r = np.random.random()
    if r < 0.01:
        x = A1 @ x + b1
    elif r < 0.86:
        x = A2 @ x + b2
    elif r < 0.93:
        x = A3 @ x + b3
    else:
        x = A4 @ x + b4
    lis.append(x.T[0])

df = pd.DataFrame(lis, columns=['x', 'y'])
df.plot.scatter('x', 'y', s=1, alpha=0.1, c='green')
plt.show()

Total running time of the script: ( 0 minutes 1.290 seconds)

Gallery generated by Sphinx-Gallery