from numpy import linspace,meshgrid,column_stack import matplotlib.cm as cmap import matplotlib.pyplot as plt def plane(coord,a,b,c): """Function to define a plane""" return c-(coord[0]*a+coord[1]*b) coeefs=[1,-0.5,-1] col=linspace(-10,10,6) X,Y=meshgrid(col,col) Z=plane((X,Y),*coeefs)+normal(size=X.shape,scale=7.0) d=Data(column_stack((X.ravel(),Y.ravel(),Z.ravel())),filename="Fitting a Plane",setas="xyz") d.column_headers=["X","Y","Z"] d.figure(projection="3d") d.plot_xyz(plotter="scatter",c=cmap.jet(d.z)) d.curve_fit(plane,[0,1],2,result=True) d.setas="xy.z" d.plot_xyz(linewidth=0,cmap=cmap.jet) txt="$z=c-ax+by$\n" txt+="\n".join([d.format("plane:{}".format(k),latex=True) for k in ["a","b","c"]]) ax=plt.gca(projection="3d") ax.text(15,5,-50,txt) d.draw()
def plane(coord, a, b, c): """Function to define a plane""" return c - (coord[0] * a + coord[1] * b) coeefs = [1, -0.5, -1] col = linspace(-10, 10, 6) X, Y = meshgrid(col, col) Z = plane((X, Y), *coeefs) + normal(size=X.shape, scale=7.0) d = Data( column_stack((X.ravel(), Y.ravel(), Z.ravel())), filename="Fitting a Plane", setas="xyz", ) d.column_headers = ["X", "Y", "Z"] d.figure(projection="3d") d.plot_xyz(plotter="scatter") popt, pcov = d.curve_fit(plane, [0, 1], 2, result=True) d.setas = "xy.z" d.plot_xyz(linewidth=0, cmap=cmap.jet) txt = "$z=c-ax+by$\n" txt += "\n".join([d.format("plane:{}".format(k), latex=True) for k in ["a", "b", "c"]]) ax = plt.gca(projection="3d") ax.text(15, 5, -50, txt) d.draw()
"""Simple use of lmfit to fit data.""" from Stoner import Data from numpy import linspace,exp,random #Make some data x=linspace(0,10.0,101) y=2+4*exp(-x/1.7)+random.normal(scale=0.2,size=101) d=Data(x,y,column_headers=["Time","Signal"],setas="xy") d.plot(fmt="ro") # plot our data #Do the fitting and plot the result fit = d.lmfit(lambda x,A,B,C:A+B*exp(-x/C),result=True,header="Fit",A=1,B=1,C=1) d.setas="x.y" d.labels=[] d.plot(fmt="b-") # Make nice label of the parameters text=r"$y=A+Be^{-x/C}$"+"\n\n" text+="\n".join([d.format(k,latex=True) for k in ["Model:A","Model:B","Model:C"]]) d.text(5,4,text,fontdict={"size":"x-small"})
"""Function to define a plane""" return c - (coord[0] * a + coord[1] * b) coeefs = [1, -0.5, -1] col = linspace(-10, 10, 8) X, Y = meshgrid(col, col) Z = plane((X, Y), *coeefs) + normal(size=X.shape, scale=2.0) d = Data( column_stack((X.ravel(), Y.ravel(), Z.ravel())), filename="Fitting a Plane", setas="xyz", ) d.column_headers = ["X", "Y", "Z"] d.figure(projection="3d") d.plot_xyz(plotter="scatter") popt, pcov = d.curve_fit(plane, [0, 1], 2, result=True) d.setas = "xy.z" d.plot_xyz(linewidth=0, cmap=cmap.jet) txt = "$z=c-ax+by$\n" txt += "\n".join( [d.format("plane:{}".format(k), latex=True) for k in ["a", "b", "c"]]) ax = plt.gca(projection="3d") ax.text(15, 5, -50, txt) d.draw()