我有一个读取 50 个文本文件的代码,每个文件生成一个直方图,并且为每个直方图拟合一个高斯。然后获取每个高斯下的区域并根据时间绘制(本质上是它乘以 18 的文本文件)。
然后我想在原始数据中单独操作这些数据。我可以获取代码以从图表中打印出 y 值,但我正在努力将其更改为可以复制并粘贴到原始电子表格格式的表单。
打印出来的是:
77.78630383833547, 62.92926582239441, 63.84025706577048, 55.489066870438165, 38.60797989548756, 40.771390484048545, 14.679073842876978, 33.95959972488966, 29.41960790300141, 32.93241034391399, 30.927428194781815, 31.086396885182356, 21.52771899125612, 4.27684299160886, 6.432975528727562, 7.500376934048583, 18.730555740591637, 4.355896959987761, 11.677509915219987, 12.865482314301719, 0.6120306267606219, 12.614420497451556, 2.2025029753442404, 9.447046999592711, 4.0688197216393425, 0.546672901996845, 1.12780050608251, 2.2030852358874635, 2.202804718915858, 0.5726686031033587, 0.5465322281618783, 0.5185100682386156, 0.575055917739342, 0.5681697592593679
完整代码在这里,如果需要的话。我猜另一个变量我只写了 0, 18, 36, 54 ......所以可能不需要 python!
更新 请求了相关的代码示例。我认为这是一个最小的例子。我正在尝试输出的相关变量是安培。我不介意这是通过保存文件(可能是首选)还是在 IDE 控制台中完成的。
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from numpy import exp, loadtxt, pi, sqrt, random, linspace
from lmfit import Model
import glob, os
## Define gaussian
def gaussian(x, amp, cen, wid):
"""1-d gaussian: gaussian(x, amp, cen, wid)"""
return (amp / (sqrt(2*pi) * wid)) * exp(-(x-cen)**2 / (2*wid**2))
## Define exponential decay
def expdecay(x, t, A):
return A*exp(-x/t)
## Define constants
fileToRun = 'Run0'
location = 'ControlRoom6'
stderrThreshold = 10
minimumAmplitude = 0.1
approxcen = 780
DeadTime = 3
LiveTime = 15
## Get location of files to be loaded
## Define paramaters
amps = []; ampserr = []; ts = []
folderToAnalyze = baseFolder + fileToRun + '\\'
## Generate the time array and coincidence plots
for n in range(0, numfiles):
## Load text file
x = np.linspace(0, 8191, 8192)
finalprefix = str(n).zfill(3)
fullprefix = folderToAnalyze + prefix + finalprefix
y = loadtxt(fullprefix + ".Spe", skiprows= 12, max_rows = 8192)
## Make figure and label
fig, ax = plt.subplots(figsize=(15,8))
## Plot data
ax.bar(x, y)
ax.set_xlim(600,960)
# Fit data to a gaussian
gmodel = Model(gaussian)
result = gmodel.fit(y, x=x, amp=8, cen=approxcen, wid=1)
## Append to list if error in amplitude and amplitude itself is within reasonable bounds
if result.params['amp'].stderr < stderrThreshold and result.params['amp'] > minimumAmplitude:
amps.append(result.params['amp'].value)
ampserr.append(result.params['amp'].stderr)
ts.append(MaestroT*n)
## Plot decay curve
print(amps)
fig, ax = plt.subplots()
ax.errorbar(ts, amps, yerr= 2*np.array(ampserr), fmt="ko-", capsize = 5, capthick= 2, elinewidth=3, markersize=5)
plt.xlabel('Time', fontsize=14)
plt.ylabel('Peak amplitude', fontsize=14)
plt.title("Decay curve of P-31 by $β^+$ emission", fontsize=14)
## Fit decay curve