Python 绘制大量数据点
我遇到了一个奇怪的问题:当我将一个非线性方程中的大量数据点存储到3个数组(x、y和z)中,然后尝试将它们绘制在二维图中(θ-φ图,因此是二维图) 我试图通过每20个数据点的采样点来消除需要绘制的点,因为z数据是近似周期性的。我选取了z值略高于零的点,以确保每个周期选取一个点 当我尝试做上述工作时,问题出现了。我在图表上得到的点数非常有限,大约为152点,不管我如何改变我的初始数据点数(当然,只要它超过了一定数量)。 我怀疑可能是我错误地使用了某些命令,或者阵列的容量比我预期的小(似乎不太可能),有人能帮我找出问题出在哪里吗Python 绘制大量数据点,python,matplotlib,Python,Matplotlib,我遇到了一个奇怪的问题:当我将一个非线性方程中的大量数据点存储到3个数组(x、y和z)中,然后尝试将它们绘制在二维图中(θ-φ图,因此是二维图) 我试图通过每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')
def drawstaticplot(m,n,d_n,n_o):
计数器=0
对于范围(0,m)内的i:
n=向量rungekutta1(n,d_n)
d_n=向量rungekutta2(n,d_n,i)
x1=n[0]
y1=n[1]
z1=n[2]
如果i%20==0:
xarray.append(x1)
yarray.append(y1)
zarray.append(z1)
对于范围为(0,(m/20)-20的j:
如果((zarray[j]-n_o)>0)和((zarray[j+1]-n_o),您可以使用此处建议的方法:
e、 g.直方图中的点过多和密度较低的点。
或者也可以对matplotlib使用光栅化标志,这会加快matplotlib的速度。
我仍在调查您的问题,但请注意:
您可以执行以下操作,而不是循环并附加到数组:
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])
选择每个第n个元素:
因此,不是计算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])
def drawstaticplot(新的、新的、新的、新的):
#基于n创建存储,
#注意,我假设rungekutta返回n的大小为new_m,
#但你可以改变它。
x、 y,z=np.zero(n.shape[0]),np.zero(n.shape[0]),np.zero(n.shape[0])
对于idx,在enumerate(new#m)中使用itme:#注意函数enumerate,让它成为您的朋友!
n=向量rungekutta1(n,d_n)
d_n=vector.rungekutta2(n,d_n,ite,)
x1=n[0]
y1=n[1]
z1=n[2]
#如果i%20==0:#我们不需要检查第20个元素,m已经被过滤了。。。
xarray[idx]=n[0]
雅雷[idx]=n[1]
zarray[idx]=n[2]
#第二个循环是否必要?
if((zarray[idx]-n_o)>0)和((zarray[j+1]-n_o)嗯,你的缩进在我看来很奇怪——应该是四个空格,但看看你的if((zarray[j]
行是。使用-tt
运行程序,例如python-tt yourprogramname.py
,以排除制表符/空格错误。我也不确定我是否理解j
循环在做什么。I-loop创建3个数组,每20个点采样一个点,然后将其放入数组中。j-loop只考虑那些选中的点,因此其尺寸为“m/20”。我想为z的每个周期选择一个点,所以在z数据从正值变为负值的情况下,我选择略高于零的z数据。你有别名问题。按其他顺序循环,我打赌你会得到更多的点。另外,请考虑使用numpy内置项。@DSM:你是对的,我不是思考清楚。j-loop不直接受i-loop的影响,我不需要说得太远。我不确定这是否有帮助,但我会在这里放上我可运行的完整代码。:p这个想法是绘制一个小的旋转顶端动画,并尝试分析它的运动。感谢你提供了选择第n个数字的有用技巧,但是我所有的n和d\n表示旋转顶端的位置和速度矢量。在每一步中,它都是由最后一步生成的,所以我恐怕还需要计算完整的序列。@Yi ShiuanHuang,它确实有用,它省去了很多猜测。谢谢你的帮助,我对编程还是新手。我一定已经犯了很多noob错误。:p和一些代码b从其他地方借来的,所以在某种程度上有点混乱。