Python中的平滑图问题
作者:互联网
我一直在尝试平滑由于我使用的采样率及其计数而引起噪音的图.我一直在这里使用帮助-主要是Plot smooth line with PyPlot(尽管我找不到“样条”函数,所以我改用UnivarinteSpline)
但是,无论我做什么,都会因pyplot错误(“ x和y的长度不同”)或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
解决方法:
让我们从范围(0,2500,50)中的x开始分析代码:
>您将z定义为由0填充的6位数字的字符串.您应该真正使用某种字符串格式,例如z =“ {0:06d}”.format(x)或z =“?d”%x,而不要使用这些测试.
>在循环结束时,图章将包含(2500 // 50)= 50个元素.
>您检查文件路径是否存在,然后打开并读取它,但从不关闭它.一种更Python化的方法是:
try:
with open(path,"r") as f:
do...
except IOError:
do something else
使用with语法,文件将自动关闭.
> pr1和pr2可能是一维数组,对吗?您可以通过以下方式真正简化p1和p2列表的构造:
p1 = pr1.tolist()
p2 = pr2.tolist()
>您的列表a1,a2具有相同的大小:您可以将range(len(a ..)-1)循环中的for元素合并为一个.您也可以使用np.diff函数.
>在range(…)循环中x的末尾,finger将有50个元素减去丢失的文件数.由于您没有告诉我们文件丢失的情况,因此图章和手指列表可能没有相同数量的元素,这会使scipy.UnivariateSpline崩溃.一个简单的解决方法是仅在定义了路径文件的情况下更新您的标记列表(这样,它始终具有与finger相同的元素数).
>您的stampnew数组有300个元素,而您的图章和手指最多只能有50个元素.这是第二个问题,权重数组(stampnew)的大小必须与输入的大小相同.
>您最终将尝试绘制Fingernew vs Stamp.问题在于fingernew不是数组,它是UnivariateSpline的实例.您仍然需要计算一些实际点,例如使用fingernew(stamp),然后在绘图函数中使用它们.
标签:smoothing,graph,python 来源: https://codeday.me/bug/20191101/1983496.html