2015-02-23 44 views
0

我需要巴恩斯利蕨類植物的幫助。我沒有得到所有的葉子圖片,卻得到了錯誤的運行時間消息,當我跑我的代碼,我知道我很接近,但需要一些幫助巴恩斯利蕨類植物蟒蛇語

#! /usr/bin/env python 


import matplotlib.pyplot as plt 
import random 

# starting values 
x = 0.5 
y = 0.0 

for i in range(10000): 
     rand = (random.randrange(0,1)) 
     if (rand < 0.02): 
       x = 0.5 
       y = 0.27*y 
     if ((0.02 <= rand) and (rand <= 0.17)): 
       x = -0.139*x + 0.263*y + 0.57 
       y = 0.246*x + 0.224*y - 0.036 
     if ((0.17 < rand) and (rand <= 0.3)): 
       x = 0.17*x - 0.215*y + 0.408 
       y = 0.222*x + 0.176*y + 0.0893 
     if ((0.3 < rand) and (rand < 1.0)): 
       x = 0.781*x + 0.034*y + 0.1075 
       y = -0.032*x +0.739*y + 0.27 


plt.plot(x,y,'.') 
plt.show() 

回答

2

有你的代碼有兩個主要問題:

  1. random.randrange(0, 1)總是產生0。嘗試使用random.random()代替。

  2. 您需要縮進plot命令才能在每次迭代中執行該命令。

此外,最好先收集一個大陣列中的所有座標,然後調用plot命令。這可能會更節省時間,因爲您只需調用渲染引擎一次。

enter image description here

0

你只有一個點,因爲你只給一個x和一個yplot。您需要將您的值放入列表中:

# starting values 
x = 0.5 
y = 0.0 
# Lists of coordinates to plot 
xlist = [x] 
ylist = [y] 
for i in range(10000): 
     rand = (random.randrange(0,1)) 
     if (rand < 0.02): 
       x = 0.5 
       y = 0.27*y 
     if ((0.02 <= rand) and (rand <= 0.17)): 
       x = -0.139*x + 0.263*y + 0.57 
       y = 0.246*x + 0.224*y - 0.036 
     if ((0.17 < rand) and (rand <= 0.3)): 
       x = 0.17*x - 0.215*y + 0.408 
       y = 0.222*x + 0.176*y + 0.0893 
     if ((0.3 < rand) and (rand < 1.0)): 
       x = 0.781*x + 0.034*y + 0.1075 
       y = -0.032*x +0.739*y + 0.27 
     xvec.append(x) 
     yvec.append(y) 
plt.plot(x, y, '.') 
plt.show() 
0

使用random.uniform(0,1)而不是random.randrange(0,1)。它將返回0和1之間的浮點數隨機數

import random 
import matplotlib.pyplot as plt 
# starting values 
x = 0.5 
y = 0.0 
# Lists of coordinates to plot 
xvec = [x] 
yvec = [y] 
for i in range(10000): 
     rand = random.uniform(0, 1) 
     if (rand < 0.02): 
       x = 0.5 
       y = 0.27*y 
     if ((0.02 <= rand) and (rand <= 0.17)): 
       x = -0.139*x + 0.263*y + 0.57 
       y = 0.246*x + 0.224*y - 0.036 
     if ((0.17 < rand) and (rand <= 0.3)): 
       x = 0.17*x - 0.215*y + 0.408 
       y = 0.222*x + 0.176*y + 0.0893 
     if ((0.3 < rand) and (rand < 1.0)): 
       x = 0.781*x + 0.034*y + 0.1075 
       y = -0.032*x +0.739*y + 0.27 
     xvec.append(x) 
     yvec.append(y) 
plt.plot(xvec, yvec, '.') 
plt.show()