%matplotlib notebook import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import MultiCursor from scipy.optimize import leastsq d=np.pi/180 def Fun(p,x): a1,a2 = p return np.arctan(np.sin(74.2*d)*np.np.sin((x-a1)*d)/(np.cos(74.2*d)*np.sin(66.9*d)-np.cos(66.9*d)*np.sin(74.2*d)*np.cos((x-a1)*d)))+a2 def error(p,x,y): return Fun(p,x)-y a=np.loadtxt('/home/dgg/Downloads/test.txt') k=a[:,2];I=a[:,3];q=a[:,4]; u=a[:,5]; v=a[:,6]; PA=a[:,7] c=PA[693:1024] d=(c-178.6927).reshape(331,1) f=PA[0:693].reshape(693,1) pa=np.vstack((f,d)).reshape(1024,1) k1=k[606:730].reshape(124,1) I1=I[606:730].reshape(124,1) q1=q[606:730].reshape(124,1) u1=u[606:730].reshape(124,1) v1=v[606:730].reshape(124,1) phi=(k1/1023*360).reshape(124,1) psi=pa[606:730].reshape(124,1) x=phi y=psi p0 = [235,-60] # 拟合的初始参数设置 para = leastsq(error, p0, args=(x,y)) # 进行拟合 y_fitted = Fun (para[0],x) # 画出拟合后的曲线 fig, (ax1, ax2)=plt.subplots(2,sharex=True,figsize=(7,9)) fig_ratio=2 plt.figure ax2.plot(phi, I1) ax2.plot(phi,np.sqrt(q1**2+u1**2)) ax2.plot(phi,v1) ax1.plot(x,y,'r', label = 'Original curve') ax1.plot(x,y_fitted,'-b', label ='Fitted curve') multi=MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1) plt.subplots_adjust(wspace=0, hspace=0) plt.show() plt.figure def error(p,x,y): return Fun(p,x)-y a=np.loadtxt('/home/dgg/Downloads/test.txt') k=a[:,2];I=a[:,3];q=a[:,4]; u=a[:,5]; v=a[:,6]; PA=a[:,7] c=PA[693:1024] d=(c-178.6927).reshape(331,1) f=PA[0:693].reshape(693,1) pa=np.vstack((f,d)).reshape(1024,1) k1=k[606:730].reshape(124,1) I1=I[606:730].reshape(124,1) q1=q[606:730].reshape(124,1) u1=u[606:730].reshape(124,1) v1=v[606:730].reshape(124,1) phi=(k1/1023*360).reshape(124,1) psi=pa[606:730].reshape(124,1) x=phi y=psi p0 = [235,-60] # 拟合的初始参数设置 para = leastsq(error, p0, args=(x,y)) # 进行拟合 y_fitted = Fun (para[0],x) # 画出拟合后的曲线 fig, (ax1, ax2)=plt.subplots(2,sharex=True,figsize=(7,9)) fig_ratio=2 plt.figure ax2.plot(phi, I1) ax2.plot(phi,np.sqrt(q1**2+u1**2)) ax2.plot(phi,v1) ax1.plot(x,y,'r', label = 'Original curve') ax1.plot(x,y_fitted,'-b', label ='Fitted curve') multi=MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1) plt.subplots_adjust(wspace=0, hspace=0) plt.show() plt.figure--------------------------------------------------------------------------- AttributeError Traceback (most recent call last)in26 y=psi 27 p0 = [235,-60] # 拟合的初始参数设置 ---> 28 para = leastsq(error, p0, args=(x,y)) # 进行拟合 29 y_fitted = Fun (para[0],x) # 画出拟合后的曲线 30 fig, (ax1, ax2)=plt.subplots(2,sharex=True,figsize=(7,9)) ~/soft/lib/python3.7/site-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag) 408 if not isinstance(args, tuple): 409 args = (args,) --> 410 shape, dtype = _check_func('leastsq', 'func', func, x0, args, n) 411 m = shape[0] 412 ~/soft/lib/python3.7/site-packages/scipy/optimize/minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape) 22 def _check_func(checker, argname, thefunc, x0, args, numinputs, 23 output_shape=None): ---> 24 res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) 25 if (output_shape is not None) and (shape(res) != output_shape): 26 if (output_shape[0] != 1):in error(p, x, y) 9 return np.arctan(np.sin(74.2*d)*np.np.sin((x-a1)*d)/(np.cos(74.2*d)*np.sin(66.9*d)-np.cos(66.9*d)*np.sin(74.2*d)*np.cos((x-a1)*d)))+a2 10 def error(p,x,y): ---> 11 return Fun(p,x)-y 12 a=np.loadtxt('/home/dgg/Downloads/test.txt') 13 k=a[:,2];I=a[:,3];q=a[:,4]; u=a[:,5]; v=a[:,6]; PA=a[:,7]in Fun(p, x) 7 def Fun(p,x): 8 a1,a2 = p ----> 9 return np.arctan(np.sin(74.2*d)*np.np.sin((x-a1)*d)/(np.cos(74.2*d)*np.sin(66.9*d)-np.cos(66.9*d)*np.sin(74.2*d)*np.cos((x-a1)*d)))+a2 10 def error(p,x,y): 11 return Fun(p,x)-y ~/soft/lib/python3.7/site-packages/numpy/__init__.py in __getattr__(attr) 213 else: 214 raise AttributeError("module {!r} has no attribute " --> 215 "{!r}".format(__name__, attr)) 216 217 def __dir__(): AttributeError: module 'numpy' has no attribute 'np'
In[]: