2

我遇到了一个奇怪的问题:当我将非线性方程中的大量数据点存储到 3 个数组(x、y 和 z)中,然后尝试将它们绘制在 2D 图中(theta-phi plot,因此它的 2D )。

我试图通过每 20 个数据点的采样点来消除需要绘制的点,因为 z 数据是近似周期性的。我选择了 z 值略高于零的那些点,以确保我为每个周期选择一个点。

当我尝试执行上述操作时,问题就出现了。我在图表上只得到了非常有限的点数,大约 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 回答