2012-06-29 73 views
4

我想知道是否有人有一個想法,爲什麼下面的代碼不按照圖形用戶界面按鈕後顯示一個線條在它的圖形。我想創建一個程序,在點擊一個按鈕導入一組數據後,執行一長串命令。其中一個命令是在同一窗口內的圖表上顯示光譜數據。以下是我迄今爲止:如何刷新Tkinter窗口中的matplotlib圖?

# import modules that I'm using 
import matplotlib 
matplotlib.use('TKAgg') 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg 
from matplotlib.figure import Figure 
import matplotlib.pyplot as pltlib 
import Tkinter 
from Tkinter import * 
import numpy as np 
import scipy as sc 
#import matplotlib.pyplot as pltlib 
# lmfit is imported becuase parameters are allowed to depend on each other along with bounds, etc. 
from lmfit import minimize, Parameters, Minimizer 



#Make object for application 
class App_Window(Tkinter.Tk): 
    def __init__(self,parent): 
     Tkinter.Tk.__init__(self,parent) 
     self.parent = parent 
     self.initialize() 
    def initialize(self): 
     button = Tkinter.Button(self,text="Open File",command=self.OnButtonClick).pack(side=Tkinter.TOP) 
     self.canvasFig=pltlib.figure(1) 
     Fig = matplotlib.figure.Figure(figsize=(5,4),dpi=100) 
     FigSubPlot = Fig.add_subplot(111) 
     x=[] 
     y=[] 
     self.line1, = FigSubPlot.plot(x,y,'r-') 
     self.canvas = matplotlib.backends.backend_tkagg.FigureCanvasTkAgg(Fig, master=self) 
     self.canvas.show() 
     self.canvas.get_tk_widget().pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1) 
     self.canvas._tkcanvas.pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1) 
     self.resizable(True,False) 
     self.update() 
    def refreshFigure(self,x,y): 
     self.line1.set_xdata(x) 
     self.line1.set_ydata(y) 
     self.canvas.draw() 
    def OnButtonClick(self): 
     # file is opened here and some data is taken 
     # I've just set some arrays here so it will compile alone 
     x=[] 
     y=[] 
     for num in range(0,1000):x.append(num*.001+1) 
     # just some random function is given here, the real data is a UV-Vis spectrum 
     for num2 in range(0,1000):y.append(sc.math.sin(num2*.06)+sc.math.e**(num2*.001)) 
     X = np.array(x) 
     Y = np.array(y) 
     self.refreshFigure(X,Y) 

if __name__ == "__main__": 
    MainWindow = App_Window(None) 
    MainWindow.mainloop() 

回答

9

這是因爲x軸& y軸的範圍不會更改爲新的數據的範圍,改變你的refreshFigure如下:

def refreshFigure(self,x,y): 
    self.line1.set_data(x,y) 
    ax = self.canvas.figure.axes[0] 
    ax.set_xlim(x.min(), x.max()) 
    ax.set_ylim(y.min(), y.max())   
    self.canvas.draw()