def error(x, y, color="C0", zorder=20, fmt=" ", bottom=None, top=None, left=None, right=None, **kwargs): assert len(x) == len(y) mask = np.repeat(True, len(x)) if left is not None: mask &= x >= left if right is not None: mask &= x <= right if bottom is not None: mask &= y >= bottom if top is not None: mask &= y <= top x = x[mask] y = y[mask] plt.errorbar(unv(x), unv(y), xerr=usd(x) if any(usd(x)) else None, yerr=usd(y) if any(usd(y)) else None, fmt=fmt, color=color, zorder=zorder, **kwargs)
def fitspaceY(datax, datay, function, p0=None, xfit=None, range=None, **kwargs): if range is None: range = min(unv(datax)), max(unv(datax)) if xfit is None: xfit = np.linspace(*range, **kwargs) p = fitY(datax, datay, function, p0=p0, **kwargs) return xfit, function(xfit, *p), p
def fit(x, y, sigma=1, alpha=0.4, alphaData=0.7, label=None, color="C1", zorder=10, filltype="y", **kwargs): plt.plot(unv(x), unv(y), alpha=alphaData, label=label + r"$\pm %s\sigma$" % sigma if label else label, color=color, zorder=zorder, **kwargs) if filltype == "y": plt.fill_between(unv(x), unv(y) - sigma * usd(y), unv(y) + sigma * usd(y), alpha=alpha, color=color, zorder=zorder, **kwargs) if filltype == "x": plt.fill_betweenx(unv(y), unv(x) - sigma * usd(x), unv(x) + sigma * usd(x), alpha=alpha, color=color, zorder=zorder, **kwargs)
def fitspaceXY(datax, datay, function, p0, xfit=None, range=None, num=50, functionUnc=None, **kwargs): if range is None: range = min(unv(datax)), max(unv(datax)) if xfit is None: xfit = np.linspace(*range, num) if functionUnc is None: functionUnc = function p = fitXY(datax, datay, function, p0=p0, **kwargs) return xfit, functionUnc(xfit, *p), p
def fitY(datax, datay, function, p0=None, epsfcn=0.0001, maxfev=10000, **kwargs): yerr = usd(datay) if uncertain(datay) else None pfit, pcov = curve_fit(function, unv(datax), unv(datay), p0=p0, sigma=usd(yerr), epsfcn=epsfcn, maxfev=maxfev, **kwargs) perr = np.sqrt(np.diag(pcov)) return unp.uarray(pfit, perr)
time = np.array(messung3["Messzeit"]) counts = stat(messung3["Counts"]) / time messung3 = winkel, counts # %% 1. Verzögerungsdauer def gauss(x, A, x0, sigma, y0): return A * np.exp(-((x-x0)/sigma)**2/2) + y0 def gaussUnc(x,A,x0,sigma,y0): return A * unp.exp(-((x-x0)/sigma)**2/2) + y0 fig = plt.Figure(figsize=fullscreen) dt, counts = messung1 plt.errorbar(unv(dt), unv(counts), xerr=usd(dt), yerr=usd(counts), fmt=" ", color="C0", zorder=10, label="Messung") p0 = [1200, 0.333, 0.1, 0] wechselT = 0.199 xdata, ydata = zip(*[(t,c) for t,c in zip(dt,counts) if t >= wechselT]) p = (A,x0,d,y0) = fit.fitXY(xdata, ydata, gauss, p0) eff = gaussUnc(0.333, *p) / A print("eff = %s" % eff) latex.SI(eff, "", data_out, "messung1_eff") latex.SI(A.format("3f"), "", data_out, "messung1_A") latex.SI(x0*C.kilo, "\\nano\\second", data_out, "messung1_x0") latex.SI(d*C.kilo, "\\nano\\second", data_out, "messung1_d") latex.SI(y0, "", data_out, "messung1_y0") xfit1 = np.linspace(wechselT, max(unv(dt)), num=250)
def interpolatespace(datax, datay, range=None, **kwargs): if range is None: range = min(unv(datax)), max(unv(datax)) xfit = np.linspace(*range, **kwargs) return xfit, interpolate(datax, datay)(xfit)
def fitXY(datax, datay, function, p0, **kwargs): model = Model(lambda p, x: function(x, *p)) realdata = RealData(unv(datax), unv(datay), sx=usd(datax), sy=usd(datay)) odr = ODR(realdata, model, beta0=p0, **kwargs) out = odr.run() return unp.uarray(out.beta, out.sd_beta)