import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit #get data from txt, usecols=0 is B_x, 1 is B_y, 2 is B_z B=np.genfromtxt('Data.txt', usecols=1, unpack=True) #just determining calibrating parameters from several tries ratio=np.mean([0.49275,0.47778,0.48105,0.51098,0.49541,0.5094,0.4783,0.51914]) dratio=np.std([0.49275,0.47778,0.48105,0.51098,0.49541,0.5094,0.4783,0.51914]) offset=np.mean([7.12939,6.99607,6.8554,6.13588,7.06523,6.78745,3.4305,4.1762]) doffset=np.std([7.12939,6.99607,6.8554,6.13588,7.06523,6.78745,3.4305,4.1762]) print('ratio:',np.round(ratio,3),'+/-',np.round(dratio,3)) print('offset:',np.round(offset,3),'+/-',np.round(doffset,3)) #calibrated new values B_cal=(-1)*(ratio*B-ratio*offset) dB_cal=np.sqrt((B*dratio-offset*dratio)**2+(ratio*1.615)**2+(ratio*doffset)**2) # exemplary plotting and fitting, analyse calibrated values whichever way you want print(B_cal) plt.errorbar(x,B_cal,dB-cal) plt.ylabel('G') plt.xlabel('mm') plt.title('Gradient field z-axis 4A AHH, 1A HH') def linear(x,m,b): return m*x+b popt,pcov=curve_fit(linear,x,B_cal,sigma=dB_cal) plt.plot(x,linear(x,*popt),label='measurement fit') print('Gradient:',popt[0]*10,'+/-',np.sqrt(pcov[0][0])*10) plt.show()