用Python平滑图形的问题
我一直在试图平滑由于我正在使用的采样率以及它的计数而产生噪声的图。我一直在使用这里的帮助 - 主要是Plot smooth line with PyPlot(虽然我找不到“样条”功能,所以我使用UnivarinteSpline
代替)用Python平滑图形的问题
但是,无论我做什么,我总是收到错误的pyplot错误"x and y are not of the same length"
,或者scipi.UnivariateSpline
的值为w
,这是不正确的。我不确定如何解决这个问题(不是真正的Python人员!)我已经附上了代码,尽管它只是最后导致问题的绘图位。由于
import os.path
import matplotlib.pyplot as plt
import scipy.interpolate as sci
import numpy as np
def main():
jcc = "0050"
dj = "005"
l = "060"
D = 20
hT = 4 * D
wT1 = 2 * D
wT2 = 5 * D
for jcm in ["025","030","035","040","045","050","055","060"]:
characteristic = "LeadersOnly/Jcm" + jcm + "/Jcc" + jcc + "/dJ" + dj + "/lambda" + l + "/Seed000"
fingertime1 = []
fingertime2 = []
stamp =[]
finger=[]
for x in range(0,2500,50):
if x<10000:
z=("00"+str(x))
if x<1000:
z=("000"+str(x))
if x<100:
z=("0000"+str(x))
if x<10:
z=("00000"+str(x))
stamp.append(x)
path = "LeadersOnly/Jcm" + jcm + "/Jcc" + jcc + "/dJ" + dj + "/lambda" + l + "/Seed000/profile_" + str(z) + ".txt"
if os.path.exists(path):
f = open(path, 'r')
pr1,pr2=np.genfromtxt(path, delimiter='\t', unpack=True)
p1=[]
p2=[]
h1=[]
h2=[]
a1=[]
a2=[]
finger1 = 0
finger2 = 0
for b in range(len(pr1)):
p1.append(pr1[b])
p2.append(pr2[b])
for elem in range(len(pr1)-80):
h1.append((p1[elem + (2*D)]-0.5*(p1[elem]+p1[elem + (4*D)])))
h2.append((p2[elem + (2*D)]-0.5*(p2[elem]+p2[elem + (4*D)])))
if h1[elem] >= hT:
a1.append(1)
else:
a1.append(0)
if h2[elem]>=hT:
a2.append(1)
else:
a2.append(0)
for elem in range(len(a1)-1):
if (a1[elem] - a1[elem + 1]) != 0:
finger1 = finger1 + 1
finger1 = finger1/2
for elem in range(len(a2)-1):
if (a2[elem] - a2[elem + 1]) != 0:
finger2 = finger2 + 1
finger2 = finger2/2
fingertime1.append(finger1)
fingertime2.append(finger2)
finger.append((finger1+finger2)/2)
namegraph = jcm
stampnew = np.linspace(stamp[0],stamp[-1],300)
fingernew = sci.UnivariateSpline(stamp, finger, stampnew)
plt.plot(stampnew,fingernew,label=namegraph)
plt.show()
main()
的信息,数据输入文件是简单的整数列表(两份名单由制表符分隔,作为代码提示)。
以下是错误代码,我得到的一个:
0-th dimension must be fixed to 50 but got 300
error Traceback (most recent call last)
/group/data/Cara/JCMMOTFingers/fingercount_jcm_smooth.py in <module>()
116
117 if __name__ == '__main__':
--> 118 main()
119
120
/group/data/Cara/JCMMOTFingers/fingercount_jcm_smooth.py in main()
93 #print(len(stamp))
94 stampnew = np.linspace(stamp[0],stamp[-1],300)
---> 95 fingernew = sci.UnivariateSpline(stamp, finger, stampnew)
96 #print(len(stampnew))
97 #print(len(fingernew))
/usr/lib/python2.6/dist-packages/scipy/interpolate/fitpack2.pyc in __init__(self, x, y, w, bbox, k, s)
86 #_data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier
87 data = dfitpack.fpcurf0(x,y,k,w=w,
---> 88 xb=bbox[0],xe=bbox[1],s=s)
89 if data[-1]==1:
90 # nest too small, setting to maximum bound
error: failed in converting 1st keyword `w' of dfitpack.fpcurf0 to C/Fortran array
我们来分析一下你的代码了一下,从for x in range(0, 2500, 50):
开始你定义
z
为6位数字的字符串填充与0s。你应该使用一些字符串格式,如z = "{0:06d}".format(x)
或z = "%06d" % x
,而不是这些测试。在循环结束时,
stamp
将具有3210元素。-
您检查您的文件
path
是否存在,然后打开并阅读它,但您永远不会关闭它。更Python的方式是做:try: with open(path,"r") as f: do... except IOError: do something else
随着
with
语法,你的文件会自动关闭。 -
pr1
andpr2
很可能是1D数组,对不对?你真的可以简化您的p1
和p2
名单的建设为:p1 = pr1.tolist() p2 = pr2.tolist()
你的列表
a1
,a2
具有相同的大小:你可以在一个单一的一个结合您for elem in range(len(a..)-1)
循环。您也可以使用np.diff
函数。在
for x in range(...)
循环的结束,finger
将有50个元素减去丢失的文件的数量。由于您不知道在缺少文件的情况下应采取的措施,因此您的stamp
和finger
列表中可能没有相同数量的元素,这会使您的scipy.UnivariateSpline
崩溃。只有在定义了path
文件(这样,它总是具有与finger
相同数量的元素)的情况下,才能更新您的stamp
列表。您的
stampnew
数组有300个元素,当您的stamp
和finger
最多只能有50个元素。这是第二个问题,权重数组的大小(stampnew
)必须与输入的大小相同。你最终想绘制vs
stamp
。问题是是而不是阵列,它是UnivariateSpline
的实例。您仍然需要计算一些实际点数,例如fingernew(stamp)
,然后在您的plot
函数中使用。
如果没有LeadersOnly文件夹,我们无法复制,但如果您经过了所获得的回溯的确切副本,我们可能会弄清楚。另外,我对代码运行的缩进做了一些调整。 – 2012-04-22 16:33:36
不确定解决方案 - 但请将您的0填充更改为更好!初始化z的行可以替换为'z ='%06d'%x' – 2012-04-22 16:37:42
已添加到我得到的错误链之一中。... 如果我通过更改为stampnew = np .linspace(图章[0],图章[-1],50),但随后会出现更多错误,这次是关于pyplot函数 – Cara 2012-04-22 20:31:19