【发布时间】:2016-05-31 10:05:37
【问题描述】:
您好,我正在尝试用多项式或指数函数拟合我的数据,但两者都失败了。我使用的代码如下:
with open('argon.dat','r') as f:
argon=f.readlines()
eng1 = np.array([float(argon[argon.index(i)].split('\n')[0].split(' ')[0])*1000 for i in argon])
II01 = np.array([1-math.exp(-float(argon[argon.index(i)].split('\n')[0].split(' ')[1])*(1.784e-3*6.35)) for i in argon])
with open('copper.dat','r') as f:
copper=f.readlines()
eng2 = [float(copper[copper.index(i)].split('\n')[0].split(' ')[0])*1000 for i in copper]
II02 = [math.exp(-float(copper[copper.index(i)].split('\n')[0].split(' ')[1])*(8.128e-2*8.96)) for i in copper]
fig, ax1 = plt.subplots(figsize=(12,10))
ax2 = ax1.twinx()
ax1.set_yscale('log')
ax2.set_yscale('log')
arg = ax2.plot(eng1, II01, 'b--', label='Argon gas absorption at STP (6.35 cm)')
cop = ax1.plot(eng2, II02, 'r', label='Copper wall transp. (0.81 mm)')
plot = arg+cop
labs = [l.get_label() for l in plot]
ax1.legend(plot,labs,loc='lower right', fontsize=14)
ax1.set_ylim(1e-6,1)
ax2.set_ylim(1e-6,1)
ax1.set_xlim(0,160)
ax1.set_ylabel(r'$\displaystyle I/I_0$', fontsize=18)
ax2.set_ylabel(r'$\displaystyle 1-I/I_0$', fontsize=18)
ax1.set_xlabel('Photon Energy [keV]', fontsize=18)
plt.show()
这给了我 我想要做的不是将这样的数据绘制成指数曲线并将这些曲线相乘以最终得到检测器效率(我试图逐个元素相乘,但我没有有足够的数据点来获得平滑的曲线)我尝试使用 polyfit 并尝试定义一个指数函数来查看它的工作但我最终在两种情况下都得到了一条线
#def func(x, a, c, d):
# return a*np.exp(-c*x)+d
#
#popt, pcov = curve_fit(func, eng1, II01)
#plt.plot(eng1, func(eng1, *popt), label="Fitted Curve")
和
model = np.polyfit(eng1, II01 ,5)
y = np.poly1d(model)
#splineYs = np.exp(np.polyval(model,eng1)) # also tried this but didnt work
ax2.plot(eng1,y)
如有需要数据取自http://www.nist.gov/pml/data/xraycoef/index.cfm 在图 3 中也可以找到类似的工作:http://scitation.aip.org/content/aapt/journal/ajp/83/8/10.1119/1.4923022
在@Oliver 的回答之后编辑了其余部分:
我使用现有数据进行了乘法:
i = 0
eff1 = []
while i < len(arg):
eff1.append(arg[i]*cop[i])
i += 1
我最终得到的是(红色:铜,虚线蓝色:氩,蓝色:乘法) 这是我想得到的,但是通过使用曲线的函数,这将是一条我想要的平滑曲线最终得到(在@oliver 的回答下发表了关于错误或误解的评论)
【问题讨论】:
标签: python numpy matplotlib scipy curve-fitting