巴恩斯利蕨Python脚本

时间:2016-07-29 05:30:16

标签: python matplotlib plot fractals

我已经设法使用下面的代码生成了一个非常好的巴恩斯利蕨,但有些事情并不完全正确。 '干'有额外的线条,我无法找到问题。谁能解决这个问题? 巴恩斯利蕨最好的努力:

enter image description here

from PIL import Image
import random
import matplotlib.pyplot as plt
A=[]
mat=[[0.0,0.0,0.0,0.6,0.0,0.0,0.01],
    [0.85,0.04,-0.04,0.85,0.0,1.6,0.85],
    [0.2,-0.26,0.23,0.22,0.0,1.6,0.07],
    [-0.15,0.28,0.26,0.24,0.0,0.44,0.07]]
x=0.0
y=0.0
for k in range(0,100000):
    p=random.random()
    if p <= mat[0][6]:
        i=0
    elif p <= mat[0][6] + mat[1][6]:
        i=1
    elif p <= mat[0][6] + mat[1][6] + mat[2][6]:
        i=2
    else:
        i=3

    x0 = x * mat[i][0] + y * mat[i][1] + mat[i][4]
    y  = x * mat[i][2] + y * mat[i][3] + mat[i][5]
    x = x0

    ptn=[x,y]

    A.append(ptn)

plt.figure(figsize=(20,30))
plt.scatter( *zip(*A),marker='o', color='g',s=0.1)
plt.show()

1 个答案:

答案 0 :(得分:1)

矩阵中有一个拼写错误:

mat=[[0.0,0.0,0.0,0.16,0.0,0.0,0.01],
[0.85,0.04,-0.04,0.85,0.0,1.6,0.85],
[0.2,-0.26,0.23,0.22,0.0,1.6,0.07],
[-0.15,0.28,0.26,0.24,0.0,0.44,0.07]]

这解决了Grrr ......