我正在使用 Tkinter 编写一个用于在 python 中进行交互式数据拟合的程序。我想:
通过键盘手动更改拟合曲线的起始参数(即猜测参数)并在实验数据上绘制相应的曲线,以便从一个好的点开始拟合过程(ACHIEVED)
当我通过键盘更改参数时,在小部件中显示参数的实际值(未实现)
我搜索了网络,发现我的问题与文本小部件或条目小部件之间存在一些联系。
有没有人有好的解决方案?
这是修改的代码,因为我们正在拟合一个简单的指数,复制/粘贴运行并尝试(使用'r','t','y','f','g','h'键来修改参数) ..
import Tkinter as Tkinter
from Tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from numpy import * #exp,arange,sin,arctan,where
import sys
from scipy.optimize import leastsq
class App:
def __init__(self, master,slave,folnam,q_index,tab):
# Create containers
self.frame = Tkinter.Frame(master)
self.frame2 = Tkinter.Text(slave,width=10,height=10)
self.frame2.pack()
self.q_index=q_index
# Create buttons and bindings
self.button_quit = Tkinter.Button(self.frame,text="QUIT", command=master.destroy)
self.button_quit.pack(side="left")
self.button_fit = Tkinter.Button(self.frame, text= 'fit!')
self.button_fit.pack()
self.frame.bind_all("<Key>", self.decrease_a,'+') #################################
# Fill with Data
self.t = arange(1000)*.001
self.data_to_fit = exp(-self.t)
self.A=max(self.data_to_fit)-min(self.data_to_fit)
self.B=min(self.data_to_fit)
# Build Figure
fig = Figure()
self.ax = fig.add_subplot(111)
self.ax.set_ylim( min(self.data_to_fit), max(self.data_to_fit) )
self.p = [self.A,self.B,.9,.5,10.,5.]
self.line, = self.ax.plot(self.t[1:],abs(self.schultz(self.t[1:], 1., self.p)),'.-') #tuple of a single element
self.canvas = FigureCanvasTkAgg(fig,master=master)
self.ax.plot(self.t[1:],self.data_to_fit[1:])
self.canvas.show()
self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
self.frame.pack()
def schultz(self, t, q, p):
Z=.1
A, B, alpha, D, vm, sigma = p
Z = ((sigma/vm)**-2)-1.
Lambda = q*vm*t/(Z+1)
g = ((Z+1)/(Z*q*vm*t))*sin(Z*arctan(Lambda))/(1+Lambda**2)**(Z/2.)
where( abs(t)>0., g, 1.)
f = exp(-q**2*D*t)*((1.-alpha)+alpha*g)
y = A*f+B
return y
def decrease_a(self,event):
# Raise/lower amplitude with 'a', 'q' keys
if event.char=='a':
self.ax.get_ylim()
self.p[0]-= 1e10
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
if event.char=='q':
self.ax.get_ylim()
self.p[0]+=1e10
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
# Raise/lower baseline with 's', 'w' keys
if event.char=='s':
self.ax.get_ylim()
self.p[1]-= 1e10
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
if event.char=='w':
self.ax.get_ylim()
self.p[1]+= 1e10
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
# Raise/lower alpha with 'd', 'e' keys
if event.char=='d':
self.ax.get_ylim()
self.p[2]-= .05
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
if event.char=='e':
self.ax.get_ylim()
self.p[2]+= .05
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
# Raise/lower diffusion coefficient with 'f', 'r' keys
if event.char=='f':
self.ax.get_ylim()
self.p[3]-= .05
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
if event.char=='r':
self.ax.get_ylim()
self.p[3]+= .05
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
# Raise/lower average speed with 'g', 't' keys
if event.char=='g':
self.ax.get_ylim()
self.p[2]-= .05
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
if event.char=='t':
self.ax.get_ylim()
self.p[2]+= .05
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
# Raise/lower variance of speed distribution with 'h', 'y' keys
if event.char=='h':
self.ax.get_ylim()
self.p[2]-= .01
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
if event.char=='y':
self.ax.get_ylim()
self.p[2]+= .01
self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
self.line.set_xdata(self.t[1:])
self.canvas.draw()
root = Tkinter.Tk()
root2 = Tkinter.Tk()
app = App(root,root2,'/home/copo/Scrivania/correlazioni_da_fit',q_index=10, tab=False)
root.mainloop()