2

奇妙な問題に遭遇しました: 非線形方程式から 3 つの配列 (x、y、および z) に大量のデータ ポイントを格納し、それらを 2D グラフ (シータ ファイ プロット、したがってその 2D )。

z データはほぼ周期的であるため、20 データ ポイントごとにポイントをサンプリングすることで、プロットする必要のあるポイントを排除しようとしました。すべての期間で 1 つのポイントを確実に選択できるように、z 値が 0 のすぐ上にあるポイントを選択しました。

上記を実行しようとしたときに問題が発生しました。最初のデータポイント数をどのように変更したかに関係なく(もちろん、一定数を超えている限り)、グラフ上のポイント数は非常に限られており、約152ポイントしか得られませんでした。 グラフ

私が間違って使用しているコマンドであるか、配列の容量が予想よりも小さい可能性があると思われます (可能性は低いと思われます)。

def drawstaticplot(m,n, d_n, n_o):
    counter=0
    for i in range(0,m):
        n=vector.rungekutta1(n, d_n)
        d_n=vector.rungekutta2(n, d_n, i)
        x1 = n[0]    
        y1 = n[1]
        z1 = n[2]
        if i%20==0:
            xarray.append(x1)
            yarray.append(y1)
            zarray.append(z1)
    for j in range(0,(m/20)-20):
        if (((zarray[j]-n_o)>0) and ((zarray[j+1]-n_o)<0)):
           counter= counter +1
           print zarray[j]-n_o,counter
           plotthetaphi(xarray[j],yarray[j],zarray[j])

def plotthetaphi(x,y,z):
    phi= math.acos(z/math.sqrt(x**2+y**2+z**2))
    theta = math.acos(x/math.sqrt(x**2 + y**2))
    plot(theta, phi,'.',color='red')

さらに、次のSO の質問のコードを自分のコードに適用しようとしましたが、データ ポイントがランダムに生成されないことを除いて、非常によく似た結果が必要です。

4

2 に答える 2

2

シュアン、

私はまだあなたの問題を調査していますが、いくつかのメモがあります:

ループして配列に追加する代わりに、次のことができます。

n 番目ごとの要素を選択します。

# inside IPython console:
[2]: a=np.arange(0,10)

In [3]: a[::2] # here we select every 2nd element.
Out[3]: array([0, 2, 4, 6, 8])

したがって、m のすべての要素に対して runga-kutta を計算する代わりに、次のようにします。

new_m = m[::20] # select every element of m.

次のように関数を呼び出します。

def drawstaticplot(new_m,n, d_n, n_o):
    n=vector.rungekutta1(n, d_n)
    d_n=vector.rungekutta2(n, d_n, i)
    x1 = n[0]    
    y1 = n[1]
    z1 = n[2]
    xarray.append(x1)
    yarray.append(y1)
    zarray.append(z1)
    ...

大規模なデータセットの追加と反復について:

append配列全体をコピーしてから新しい要素をスタックするため、一般に遅いです。代わりに、n のサイズは既にわかっているので、次のようにすることができます。

def drawstaticplot(new_m,n, d_n, n_o):
    # create the storage based on n,
    # notice i assumed that rungekutta, returns n the size of new_m, 
    # but you can change it.
    x,y,z = np.zeros(n.shape[0]),np.zeros(n.shape[0]), np.zeros(n.shape[0])

for idx, itme in enumerate(new_m): # notice the function enumerate, make it your friend!
    n=vector.rungekutta1(n, d_n)
    d_n=vector.rungekutta2(n, d_n, ite,)
    x1 = n[0]    
    y1 = n[1]
    z1 = n[2]
    #if i%20==0: # we don't need to check for the 20th element, m is already filtered...
    xarray[idx] = n[0]
    yarray[idx] = n[1]
    zarray[idx] = n[2]
    # is the second loop necessary?
    if (((zarray[idx]-n_o)>0) and ((zarray[j+1]-n_o)<0)): 
       print zarray[idx]-n_o,counter
       plotthetaphi(xarray[idx],yarray[idx],zarray[idx])

    
于 2012-09-17T15:01:02.853 に答える
0

ここで提案されているアプローチを使用できます。 高密度領域の密度プロット、疎領域のポイント、 たとえば、ポイントが多すぎるヒストグラムと密度が低いポイントを効率的に作成します。または、matplotlib のラスター化フラグを使用して、matplotlib を高速化することもできます。

于 2012-09-17T14:58:49.417 に答える