2017-02-20 117 views
1

我試圖在用戶單擊屏幕時重繪一個繪圖。 當前,情節在第一次點擊時繪製。之後,新的情節被追加到畫布上。我想要做的是「刪除」或「清除」第一個繪圖並重新繪製或使用新數據對其進行更新。正在更新Tkinter Matplotlib圖

這是部分負責此特定情節抽獎:

class AppGUI(Tk.Frame): 

    def __init__(self, parent): 
     self.parent = parent 
     self.initGUI() 
     self.plot() 


    def initGUI(self): 
     self.vf_frame = Tk.Frame(self.parent, bd=1, relief=Tk.SUNKEN) 
     self.vf_frame.pack(side=Tk.TOP, fill="both", expand=True) 

    def plotVF(self, u, v): 
      # Canvas of VF 
      m = np.sqrt(np.power(u, 2) + np.power(v, 2)) 

      xrange = np.linspace(0, u.shape[1], u.shape[1]); 
      yrange = np.linspace(0, u.shape[0], u.shape[0]); 

      x, y = np.meshgrid(xrange, yrange) 
      mag = np.hypot(u, v) 
      scale = 1 
      lw = scale * mag/mag.max() 

      f, ax = plt.subplots() 
      h = ax.streamplot(x, y, u, v, color=mag, linewidth=lw, density=3, arrowsize=1, norm=plt.Normalize(0, 70)) 
      ax.set_xlim(0, u.shape[1]) 
      ax.set_ylim(0, u.shape[0]) 
      ax.set_xticks([]) 
      ax.set_yticks([]) 
      #cbar = f.colorbar(h, cax=ax) 
      #cbar.ax.tick_params(labelsize=5) 

      c = FigureCanvasTkAgg(f, master=self.vf_frame) 
      c.show() 
      c.get_tk_widget().pack(side=Tk.LEFT, fill="both", expand=True) 

我必須做我的班fax屬性來達到這個結果呢?爲了清楚起見,plotVF通過其他方法更新。

PS:我不能同時顯示帶註釋行的顏色條。它說'Streamplot' object has no attribute 'autoscale_None'

回答

0

您需要兩個不同的功能,一個用於啓動繪圖,另一個用於更新它。

def initplot(self): 
    f, self.ax = plt.subplots() 
    c = FigureCanvasTkAgg(f, master=self.vf_frame) 
    c.show() 
    c.get_tk_widget().pack(side=Tk.LEFT, fill="both", expand=True) 

def update(self, u, v): 
    self.ax.clear() # clear the previous plot 
    ... 
    h = self.ax.streamplot(...) 
    self.ax.set_xlim(0, u.shape[1]) 
    ...