2016-11-05 3 views
0

私はランダムウォークのプロットを作成するために使用しているいくつかのpythonコードを持っています。散歩は[-a、a]の壁に反映されます。シーケンス内の後続の値は、必要に応じて反映され、次に反映される。matplotlibの点集合の周りに一定の勾配の破線の「円錐」をプロットするにはどうすればよいですか?

r[n] = r[n-1] + Uni[-R, R] 

によって生成されます。私がしたいのは、それぞれの点の周りに[-R, R]の "不確実性の円錐"をプロットすることです。

random walk with cones

私もよ:私はコーンを追加した後、このように見えるようにプロットしたい

import matplotlib.pyplot as plt 
import random 

uni = random.uniform 

t = [] 
r = [] 

r0 = .15 # Seed for our random walk. Can be between -a and a 
a = .2 # Distance of barriers from 0. Should be in (0, 1] 
R = .04 # Height of half-cone in r-direction 
dt = 20 # Sample period 
N = 20 # Number of samples 

cone_ls = ':' 
cone_clr = 'blue'#[0, .5, .5] 

for i in range(N): 
    t.append(i*dt) 
    if i == 0: 
     r.append(r0) 
    else: 
     ''' 
     When our cone of uncertainty outpaces out barriers, 
     simply sample uniformly inside the barriers. 
     ''' 
     if(R > 2*a): 
      r.append(uni(-a, a)) 
      continue 
     rn = r[i - 1] + uni(-R, R) 
     ''' 
     If the sampled value comes above the upper barrier, 
     reflect it back below. 
     ''' 
     if(rn > a): 
      r.append(2*a - rn) 
      continue 
     ''' 
     If the sampled value comes below the lower barrier, 
     reflect it back above. 
     ''' 
     if(rn < -a): 
      r.append(-2*a - rn) 
      continue 
     ''' 
     Otherwise just append the sampled value. 
     ''' 
     r.append(rn) 
# Plot cones 
for i, pt in enumerate(r): 
    plt.plot([t[i], t[i] + dt], [pt, pt + R], linestyle=cone_ls, color=cone_clr, linewidth=2) 
    plt.plot([t[i], t[i] + dt], [pt, pt - R], linestyle=cone_ls, color=cone_clr, linewidth=2) 

plt.plot(t, r, 'ro') 
plt.plot(t, [a]*N) 
plt.plot(t, [-a]*N) 
plt.axis([min(t), max(t), -2*a, 2*a]) 
plt.xlabel('Time (min)') 
plt.ylabel('Relative Difference, r') 
plt.show() 

:ここ

は、私がこれまで持っているPythonのコードですこれを紙に含めることになるので、どんな美しいヒントもありがたいです。

編集:解決、私はちょうど個別コーンセクションをプロットするために必要な実現。

答えて

1

あなただけのコーンが終わり、データ内のすべての点

for i in range(N): 
    plt.plot([t[i]+dt,t[i],t[i]+dt],[r[i]-R,r[i],r[i]+R], color="#808080") 

用で構成されて2行は、あなたもmax(t)+dt
plt.axis([min(t), max(t)+dt, -2*a, 2*a])

enter image description hereにX制限を設定する必要がありますプロットすることができます

+0

あなたが行ったように私はそれがプロットのちょうど別のセットだと、私が実現した後、もう少し複雑な何かをやってしまいました。この1ライナーははるかに簡潔ですが、ありがとう! – ijustlovemath

関連する問題